Board logo

标题: [求助]如何用sed读取文本文件指定段位置内容? [打印本页]

作者: rongcong005     时间: 2008-6-26 21:09    标题: [求助]如何用sed读取文本文件指定段位置内容?

有一文本如下: ☆test01☆ AAAtest01=123456789 BBBtest01=45678900 CCCtest01=abcdef02 DDDDtest01=FFFFFF03 ☆test02☆ AAAtest02=123456 BBBtest02=456789 CCCtest02=abcdef DDDDtest02=FFFFFF ☆test03☆ AAAtest03=123456ll BBBtest03=456789999 CCCtest03=abcdef777 DDDDtest03=FFFFFF333 ☆test04☆ AAAtest04=123456006 BBBtest04=456789005 CCCtest04=abcdef007 DDDDtest04=FFFFFF0.7 我现在用sed命令行读任意一段如: sed -n "/☆test03☆/{n;p;}" aa.txt>hhh.txt 读取☆test03☆以下内容到hhh.txt,可是只能得到一行,如下: AAAtest03=123456ll 不知道要怎样才能得到☆test03☆以下四行的内容?即hhh.txt的内容为: AAAtest03=123456ll BBBtest03=456789999 CCCtest03=abcdef777 DDDDtest03=FFFFFF333 谢谢!!

作者: lxmxn     时间: 2008-6-27 04:53
sed -n "/☆test03☆/,${n;:,;/☆/!{p;n;T,};q}" aa.txt > hhh.txt

作者: lxmxn     时间: 2008-6-27 04:56    标题: 笨点的方法就用n读取下一行,然后q退出

sed -n "/☆test03☆/{n;p;n;p;n;p;n;p;q}" aa.txt > hhh.txt

作者: rongcong005     时间: 2008-6-27 21:59
版主!二楼和三楼的方法都用了,好像不行,输出的是空文件。。。 是不是我的sed版本不对?

作者: rongcong005     时间: 2008-6-27 22:19
是不是" q "后面应该有个" ; "? sed的语法对我来说实在是太复杂了,呵呵。。。。。。 谢谢lxmxn了!

作者: lxmxn     时间: 2008-6-29 03:35
我测试过了,2个方法都行,2楼的方法比较灵活点。 我用的sed的版本是:windows console version
E:\xm>sed -V GNU sed version 4.0.7 Copyright (C) 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law.