Chinaunix

标题: 怎么获取文件中的一段内容? [打印本页]

作者: wsryyffs66    时间: 2011-07-06 16:25
标题: 怎么获取文件中的一段内容?
文件aa.txt
[aa]
aa1
dd2
dd3
dd4

[bb]
dd5
dd6
dd7
xx8
dd9
5510

要获取
[aa]
aa1
dd2
dd3
dd4

怎么实现?
作者: ly5066113    时间: 2011-07-06 16:31
  1. awk -v RS= '/^\[aa]/' aa.txt
复制代码

作者: wsryyffs66    时间: 2011-07-06 16:38
ly5066113 发表于 2011-07-06 16:31



    好用了,但不太理解,麻烦请指导一下,谢谢!
作者: Shell_HAT    时间: 2011-07-07 01:18
回复 3# wsryyffs66


设置RS之后,行分隔符就变了,参考:
  1. awk -v RS= '$1=$1' urfile
复制代码

作者: lionfun    时间: 2011-07-07 01:27
回复 4# Shell_HAT

Shell_HAT老大,我有一个地方不太明白,就是awk -v RS= '/^\[aa]/' aa.txt以后为什就只显示:
[aa]
aa1
dd2
dd3
dd4
按你说的以[aa]作为行分隔符咯?那下面的为什么不显示
[bb]
dd5
dd6
dd7
xx8
dd9
5510
作者: yinyuemi    时间: 2011-07-07 01:35
回复 5# lionfun


   
`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.)

作者: Shell_HAT    时间: 2011-07-07 01:47
回复 5# lionfun


那不是把[a]作为列分隔符,而是把RS设置成空,类似:
  1. awk -v RS="" '/^\[aa]/' aa.txt
复制代码
详见6楼第三段
作者: lionfun    时间: 2011-07-07 01:54
本帖最后由 lionfun 于 2011-07-07 01:56 编辑

回复 7# Shell_HAT


    是啊……发现了!谢谢, 同时也谢谢ss兄!
作者: kr_zhang    时间: 2011-07-07 08:36
回复 6# yinyuemi


    弱弱问一下,这些说明都在那能看到 ?
作者: yinyuemi    时间: 2011-07-07 08:41
回复 9# kr_zhang


    info awk
    man awk
    google 'The GNU Awk User's Guide'
作者: zooyo    时间: 2011-07-07 10:22
提示: 作者被禁止或删除 内容自动屏蔽
作者: warriorys    时间: 2011-07-07 10:52
awk -v RS= '/^\[aa]/' aa.txt

小弟不才,解释一下:
将RS设置为空,匹配中间的空行;后面的匹配模式!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2