免费注册 查看新帖 |

Chinaunix

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

这个AWK怎么写,求教. [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:55:28
11 [报告]
发表于 2011-09-08 10:41 |只看该作者
回复  sbuddy


    sed -n '1~5{N;N;N;N;s/\n/,/gp}'

awk 'ORS=NR%5?",":RS'
yinyuemi 发表于 2011-09-08 08:54



    请教下,这个1~5是什么意思啊?

论坛徽章:
0
12 [报告]
发表于 2011-09-08 10:41 |只看该作者
回复 10# yinyuemi


    这个很容易混淆啊,如果是
paste - < file1 -<file2
看上去可能是把两个file粘在一起,其实标准输入只有一个,后面的一个标准输入重置了第一个标准输入,只是把file2对折了而已
如果像上面的例子针对多个标准输入是同一个文件的,seek位置也是累加偏移的,比较难理解.
我还是建议解决这种问题不要用这么高级的玩意

论坛徽章:
0
13 [报告]
发表于 2011-09-08 10:42 |只看该作者
请教下,这个1~5是什么意思啊?
xinglu1983 发表于 2011-09-08 10:41



    gnused的一个扩展

论坛徽章:
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
14 [报告]
发表于 2011-09-08 10:53 |只看该作者
回复 9# mpstat

faint,没看清楚还有,分割

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:55:28
15 [报告]
发表于 2011-09-08 10:55 |只看该作者
gnused的一个扩展
mpstat 发表于 2011-09-08 10:42



    那是什么意思呢?

论坛徽章:
0
16 [报告]
发表于 2011-09-08 11:01 |只看该作者
本帖最后由 mpstat 于 2011-09-08 11:11 编辑

回复 15# xinglu1983


first~step
This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, to select the odd-numbered lines, one would use 1~2; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying 50.

    GNU sed also supports some special two-address forms; all these are GNU extensions:

0,/regexp/
A line number of 0 can be used in an address specification like 0,/regexp/ so that sed will try to match regexp in the first input line too. In other words, 0,/regexp/ is similar to 1,/regexp/, except that if addr2 matches the very first line of input the 0,/regexp/ form will consider it to end the range, whereas the 1,/regexp/ form will match the beginning of its range and hence make the range span up to the second occurrence of the regular expression.
Note that this is the only place where the 0 address makes sense; there is no 0-th line and commands which are given the 0 address in any other way will give an error.

addr1,+N
Matches addr1 and the N lines following addr1.
addr1,~N
Matches addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.


网上找了点中文版的:
-------------- PS -1:GNU extension Collection:--------------------
  1. GNU 恰巧有许多对 POSIX sed 标准便利、省时的扩展。另外,GNU 没有 sed 早期专门版本的很多限制,如行长度限制 -- GNU 可以轻松处理任意长度的行.

  2. first~step   GNU扩展,选定 起始行~步长 的那些行。例如选择所有奇数行1~2;选择从第2行开始的,每隔3行 ‘2~3' ; 选择从第10行开始的每隔5行.

  3. /regexp/I 和 \%regexp%I  : GNU扩展,忽略大小写.

  4. 0,/regexp/ : 这是唯一一个使用0作为行号不会报错的用法,一般情况下不存在”第0行”,在任何其他命令中使用0作地址都会报错.

  5. addr1,+N  匹配addr1和它后边的N行

  6. addr1,~N  匹配addr1和它后边的行,直到输入的下一行的行号是N的倍数

论坛徽章:
0
17 [报告]
发表于 2011-09-09 07:52 |只看该作者
都是高手啊,学习了。

论坛徽章:
0
18 [报告]
发表于 2011-09-12 10:40 |只看该作者
似乎还有一个问题,就是最后一个人名后面还有逗号。可以去掉吗?

比如 John Smith, Amy Grant, Deb Mutt, Joe Lee,  Andy Hunt,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP