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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值