[20170511]sed awk抽取段落技巧.txt

简介: [20170511]sed awk抽取段落技巧.txt --//今天学习了如何使用sed awk抽取文本段落,做一个记录,使用sed我很早就知道,不常用.也做一个记录.

[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
-----------------

目录
相关文章
|
2月前
|
Perl
在Awk中,直接通过文件作为输入
在Awk中,直接通过文件作为输入
28 2
|
2月前
|
Unix Perl
`sed`命令替换文本中的单词
`sed`命令替换文本中的单词
33 3
|
索引 Perl
AWK进行简单分析文本
AWK进行简单分析文本
63 0
|
Perl
使用 awk 命令统计文本
下面只是在工作中可能会遇到的一个场景,所以记录下来,如果小伙伴有更合适的方式来统计计算,欢迎留言。
211 0