+ -
当前位置:首页 → 问答吧 → awk 的RS 为空,以什么为行分隔符?

awk 的RS 为空,以什么为行分隔符?

时间:2011-06-21

来源:互联网

awk 的RS 为空,以什么为行分隔符?


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

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

作者: ljt2k   发布时间: 2011-06-21

没明白。

作者: zooyo   发布时间: 2011-06-21

回复 ljt2k


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

作者: liion631818   发布时间: 2011-06-21

:curse::curse::curse::curse::curse:
哈哈

作者: jagel   发布时间: 2011-06-21

请看一下下面两种不同的说明:


      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.

作者: ljt2k   发布时间: 2011-06-21

本帖最后由 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应该包含一个假设条件,就是整个文件除了开头和结尾没有空白行

作者: liion631818   发布时间: 2011-06-21