ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [20170511]sed awk抽取段落技巧.txt

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

Linux操作系统 作者:lfree 时间:2017-05-11 08:27:40 0 删除 编辑

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2138877/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7971777