免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7706 | 回复: 9
打印 上一主题 下一主题

awk 的RS 为空,以什么为行分隔符? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-21 10:20 |只看该作者 |倒序浏览
awk 的RS 为空,以什么为行分隔符?


看到有的文档说是以连续的换行为“行分隔符”;
有的说以整个内容为一行?

试了一下,好像不同的环境,处理是不同的?

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
2 [报告]
发表于 2011-06-21 10:30 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
10
天蝎座
日期:2013-09-22 22:32:23程序设计版块每日发帖之星
日期:2016-08-07 06:20:00lufei
日期:2016-06-17 17:38:40程序设计版块每日发帖之星
日期:2016-06-12 06:20:002016科比退役纪念章
日期:2016-05-31 15:47:20CU十四周年纪念徽章
日期:2016-05-27 12:24:562015年亚洲杯之阿曼
日期:2015-05-03 21:01:352015年辞旧岁徽章
日期:2015-03-03 16:54:15天蝎座
日期:2013-10-20 21:05:24程序设计版块每日发帖之星
日期:2016-08-11 06:20:00
3 [报告]
发表于 2011-06-21 10:33 |只看该作者
回复 1# ljt2k


    LZ意思RS="", 书上是这么说的,RS="", 就是以空白行为记录的分隔符,就是这么设计的,不解释。。

论坛徽章:
0
4 [报告]
发表于 2011-06-21 10:34 |只看该作者

哈哈

论坛徽章:
0
5 [报告]
发表于 2011-06-21 10:35 |只看该作者
请看一下下面两种不同的说明:


      RS
            Input record separator (default is a new-line character). If the RS special variable is null, records are
            separated by sequences of one or more blank lines; leading or trailing blank lines do not result in empty
            records at the beginning or end of input; and the new-line character is always a field separator, regardless
            of the value of the FS special variable.





Normally, AWK reads one line at a time, and breaks up the line into fields. You can set the "RS" variable to change AWK's definition of a "line." If you set it to an empty string, then AWK will read the entire file into memory. You can combine this with changing the "FS" variable.

论坛徽章:
10
天蝎座
日期:2013-09-22 22:32:23程序设计版块每日发帖之星
日期:2016-08-07 06:20:00lufei
日期:2016-06-17 17:38:40程序设计版块每日发帖之星
日期:2016-06-12 06:20:002016科比退役纪念章
日期:2016-05-31 15:47:20CU十四周年纪念徽章
日期:2016-05-27 12:24:562015年亚洲杯之阿曼
日期:2015-05-03 21:01:352015年辞旧岁徽章
日期:2015-03-03 16:54:15天蝎座
日期:2013-10-20 21:05:24程序设计版块每日发帖之星
日期:2016-08-11 06:20:00
6 [报告]
发表于 2011-06-21 10:54 |只看该作者
本帖最后由 liion631818 于 2011-06-21 11:00 编辑

第一点:
当RS=""时, 开始和结尾的空白行会被忽略掉,就跟FS为默认值时,行开头和结尾的空白字符被忽略一样,
and the new-line character is always a field separator
这句话的意思是换行符是域分隔符,而且只是是其中之一,应该在并上设置的FS的值,例如

  1. cat file
  2. 1 xx
  3. 2 xx
  4. 3
  5. 4
  6. awk -vRS="" '{print $1, $2, $3}' file
  7. 1 xx 2
复制代码
此时,空白,tab和换行都是域分隔符
同理,可以理解:

  1. $ cat file
  2. 1#xx
  3. 2#xx
  4. 3
  5. 4
  6. awk -vRS="" -vFS="#" '{print $1, $2, $3}' file
  7. 1 xx 2
复制代码
第二点:
个人感觉 AWK will read the entire file into memory应该包含一个假设条件,就是整个文件除了开头和结尾没有空白行

论坛徽章:
0
7 [报告]
发表于 2011-06-21 11:19 |只看该作者
回复 6# liion631818


    谢谢解释!

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
8 [报告]
发表于 2011-06-21 15:51 |只看该作者

  1. RS == "\n"
  2.     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.
  3. RS == any single character
  4.     Records are separated by each occurrence of the character. Multiple successive occurrences delimit empty records.
  5. RS == ""
  6.     Records are separated by runs of blank lines. 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.
  7. RS == regexp
  8.     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.)
复制代码
RS=""时心空行为记录分割符

论坛徽章:
0
9 [报告]
发表于 2011-06-22 12:40 |只看该作者
学习

论坛徽章:
0
10 [报告]
发表于 2011-06-22 13:02 |只看该作者
RS 默认是换行为分隔符,文件首尾的忽略,这个和FS类似
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP