跟散仙学shell编程(十一)

上篇散仙写了关于shell里面正则的基础知识,本篇我们来特意学习下sed的高级用法。在前面散仙也写过关于sed的基础用法,如果不熟悉的,可以看散仙的前2篇博客温习一下。


sed进阶里面有sed的高级用法,使用sed来处理多行命令,下面我们来看下sed里面特殊的命令:
N:将数据流中的下一行加进来来创建一个多行组来处理
D:删除多行组中的一行
P:打印多行组中的一行

next命令小写的n,会告诉sed编辑器移动到数据流中的下一行文本,而不用重新回到命令的最开始再执行一遍:

[search@h1 821]$ cat a.txt 
a

b

c

[search@h1 821]$ sed '/^$/d' a.txt
a
b
c
[search@h1 821]$


删除空白行,现在的需求是删除头行之后的空白行,留下最后一行之前的空白行。

[search@h1 821]$ cat a.txt 
a

b

c
[search@h1 821]$ sed '/a/{n ; d}' a.txt
a
b

c
[search@h1 821]$


在这个例子中,脚本要查找含有a单词的唯一行,然后一旦找到它,n命令便会移动到下一行来判断删除,

下面看下如何合并文本:
[search@h1 821]$ cat b.txt 
this is a
this is b
hadoop
lucene
[search@h1 821]$ sed '/b/{N ; s/\n/ / }' b.txt
this is a
this is b hadoop
lucene
[search@h1 821]$


依旧是使用查询命令,组合替换命令来合并行
[search@h1 821]$ cat c.txt 
我们中国

我们的祖国
是china
[search@h1 821]$ sed 'N ; s/中国.人/中国公民/ ' c.txt
我们中国公民
我们的祖国
是china
[search@h1 821]$


下面看下多行删除命令:

[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed '/^$/{N ; /a/D}' d.txt
a

b

c
[search@h1 821]$


删除第一个空白行。

下面看下多行打印命令:


[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed -n 'N ; /a/P' d.txt

[search@h1 821]$ sed -n 'N ; /a*/P' d.txt


[search@h1 821]$

下面看下排序命令:

[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed -n '/a/!p' d.txt


b

c
[search@h1 821]$


感叹号代表取反的意思

下面看下利用正则通配,替换字符:
[search@h1 821]$ echo "this cat sleep in hist cat" | sed 's/.at/".at"/g'
this ".at" sleep in hist ".at"
[search@h1 821]$


下面我们看下,如何在原来的单词上,加上重点标记:
[search@h1 821]$ echo "this cat sleep in hist cat" | sed 's/.at/"&"/g'  
this "cat" sleep in hist "cat"
[search@h1 821]$


&符号,可以给本身的这个单词加上替换标记


下面看一些实战小例子:
加倍行间距
[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed 'G' d.txt


a


b


c

[search@h1 821]$



去掉末尾的空白行,加倍后:
[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed 'G' d.txt


a


b


c

[search@h1 821]$ sed '$!G' d.txt


a


b


c
[search@h1 821]$

格式化原来的行:
[search@h1 821]$ cat e.txt 
aa
bb

cc


ddd
[search@h1 821]$ sed '/^$/d;$!G' e.txt
aa

bb

cc

ddd
[search@h1 821]$

给文件加个行数:
[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed '=' d.txt
1

2
a
3

4
b
5

6
c
[search@h1 821]$

比较难看,换一种方式:
[search@h1 821]$ cat d.txt 

a

b

c
[search@h1 821]$ sed '=' d.txt
1

2
a
3

4
b
5

6
c
[search@h1 821]$ sed '=' d.txt | sed 'N; s/\n/ /'
1
2 a
3
4 b
5
6 c
[search@h1 821]$


只打印最后一行:

[search@h1 821]$ cat b.txt 
this is a
this is b
hadoop
lucene
[search@h1 821]$ sed -n '$p' b.txt
lucene
[search@h1 821]$

删除多余的空白行:
[search@h1 821]$ cat tt.txt 
a

bb

b

t


xy
[search@h1 821]$ sed '/./,/^$/!d' tt.txt
a

bb

b

t

xy
[search@h1 821]$


删除开头的空白行:
[search@h1 821]$ cat cc.txt 

solr

hadoop
[search@h1 821]$ sed '/./,$!d' cc.txt
solr

hadoop
[search@h1 821]$

删除html标签:
[search@h1 821]$ cat data 

<head>


this is a cat


<div>


i have a div

</div>


</head>
[search@h1 821]$ sed 's/<[^>]*>//g;/^$/d' data
this is a cat
i have a div
[search@h1 821]$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值