[20170511]sed awk抽取段落技巧.txt
--//今天学习了如何使用sed awk抽取文本段落,做一个记录,使用sed我很早就知道,不常用.也做一个记录.
$ cat aa.txt
1111
2323
=================
this is a test111
this is a test222
-----------------
dsjfdf
sdfdsf
=================
this is a test333
this is a test444
-----------------
121212
--//假设我仅仅需要,有时候开头===,结束----之间的内容.
=================
this is a test111
this is a test222
-----------------
=================
this is a test333
this is a test444
----------------
1.sed方法如下:
D:\IrisDB>sed -n "/^==/,/^--/p" bb.txt
=================
this is a test111
this is a test222
-----------------
=================
this is a test333
this is a test444
-----------------
2.awk方法比较难以理解,顺便学习学习.
D:\IrisDB>gawk "/^====/{flag=1}/^----/{flag=0}flag" bb.txt
=================
this is a test111
this is a test222
=================
this is a test333
this is a test444
--//大概意思是
--//如果查询遇到/^====/ 设置flag=1(你也可以别的字母不一定是flag)
--// 查询遇到/^----/ 设置flag=0,然后显示之间的内容.写成如下也是可以的.
D:\IrisDB>gawk "/^====/{f=1}/^----/{f=0}f" bb.txt
=================
this is a test111
this is a test222
=================
this is a test333
this is a test444
--//对比前面sed就是丢失了结尾的分隔符号,这并重要.^_^.如何保留结束内容一起显示我真不懂.
--//昏,上班查询一些文档,sed awk实际上一样.前面的awk语法有点复杂,不好理解.
R:\>gawk "/^==/,/^--/" cc.txt
=================
this is a test111
this is a test222
-----------------
=================
this is a test333
this is a test444
-----------------