免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: ly5066113
打印 上一主题 下一主题

[学习共享] 文本编辑的一点心得--awk篇 [复制链接]

论坛徽章:
0
101 [报告]
发表于 2013-12-05 14:43 |只看该作者
厉害,awk大神。。:wink:

论坛徽章:
0
102 [报告]
发表于 2013-12-06 18:06 |只看该作者
学习了 。。。。。。。。。。。。。。。。。。。。。。

论坛徽章:
0
103 [报告]
发表于 2013-12-16 19:03 |只看该作者
好厉害啊,总结的很好

论坛徽章:
2
巨蟹座
日期:2013-12-07 16:51:13天秤座
日期:2013-12-11 15:12:14
104 [报告]
发表于 2013-12-19 17:26 |只看该作者
回复 5# justlooks


    打印偶数行
awk 'i++%2'

这个没看懂!
大神有时间解释一下。

seq 10|awk 'i++%2'
2
4
6
8
10
-------------------------------
i初始是空。i++后是1. 1%2 余1,1是真,应该打印1才对啊。
2%2是0。不应该打印2啊。

论坛徽章:
3
水瓶座
日期:2014-02-20 12:02:14天蝎座
日期:2014-03-19 14:04:46水瓶座
日期:2014-07-26 20:01:02
105 [报告]
发表于 2014-02-28 12:17 |只看该作者
回复 105# lxzkenney


    i++%2  -->  先得出结果0,i再自加;如果++i%2 ,i先自加再进行%的运算

seq 10 | awk ' i++%2'  得偶数
seq 10 | awk ' ++i%2'  得奇数

论坛徽章:
0
106 [报告]
发表于 2014-03-03 16:37 |只看该作者
ly5066113@ubuntu:~$ cat urfile
1
a

2
a


3
ly5066113@ubuntu:~$ awk -v RS="" '{print "#" $0 "#"}' urfile
#1
a#
#2
a#
#3#
ly5066113@ubuntu:~$ awk -F "b" -v RS="" '{print $1}' urfile
1
2
3

[root@gateway-182 ~]# awk -F "d" -v RS="" '{print $1,$2}' urfile
1 a
2 a
3

[root@gateway-182 ~]# awk -F "c" -v RS="" '{print $2}' urfile   
a
a

[root@gateway-182 ~]# awk -F "a" -v RS="" '{print $1,$2}' urfile  
1
2
3

这里有点不明白,这里不是指定c为列的分隔符了么,为什么$2=a呢

论坛徽章:
0
107 [报告]
发表于 2014-03-03 17:35 |只看该作者
好东西哈.最近在研究awk。mark一个.

论坛徽章:
0
108 [报告]
发表于 2014-04-03 15:42 |只看该作者
,要好好看,要仔仔细细的去看,努力学会!

论坛徽章:
3
酉鸡
日期:2014-04-23 17:17:56丑牛
日期:2014-05-08 11:41:302015亚冠之德黑兰石油
日期:2015-08-03 11:14:27
109 [报告]
发表于 2014-04-04 16:03 |只看该作者
mark,学习下

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
110 [报告]
发表于 2014-04-29 18:04 |只看该作者
本帖最后由 expert1 于 2014-04-29 18:05 编辑

回复 1# ly5066113


    多谢tim的大作,不过根据官方手册,有个地方要改一下,

{code}

如果RS被设置为空,那么awk会将连续的空行作为行分隔符,与RS设置成"\n\n+"有什么区别???
1、忽略文件开头和结尾的空行。且文件不以记录分隔符结束,即最后不是空行,会将最后一个记录的尾\n去掉
2、不设置RT变量(测试未发现规律,暂时认为RT变量不可用)

{/code}

RT不可用,严格说应该是匹配为空。

这是官方手册

The following table summarizes how records are split, based on the value of RS:

RS == "\n"
Records are separated by the newline character (‘\n’). In effect, every line in the data file is a separate record, including blank lines. This is the default.

RS == any single character
Records are separated by each occurrence of the character. Multiple successive occurrences delimit empty records.

RS == ""
Records are separated by runs of blank lines. When FS is a single character, then the newline character always serves as a field separator, in addition to whatever value FS may have. Leading and trailing newlines in a file are ignored.

RS == regexp
Records are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty records. (This is a gawk extension; it is not specified by the POSIX standard.)

In all cases, gawk sets RT to the input text that matched the value specified by RS. But if the input file ended without any text that matches RS, then gawk sets RT to the null string.




您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP