上篇散仙写了关于shell里面正则的基础知识,本篇我们来特意学习下sed的高级用法。在前面散仙也写过关于sed的基础用法,如果不熟悉的,可以看散仙的前2篇博客温习一下。
sed进阶里面有sed的高级用法,使用sed来处理多行命令,下面我们来看下sed里面特殊的命令:
N:将数据流中的下一行加进来来创建一个多行组来处理
D:删除多行组中的一行
P:打印多行组中的一行
next命令小写的n,会告诉sed编辑器移动到数据流中的下一行文本,而不用重新回到命令的最开始再执行一遍:
删除空白行,现在的需求是删除头行之后的空白行,留下最后一行之前的空白行。
在这个例子中,脚本要查找含有a单词的唯一行,然后一旦找到它,n命令便会移动到下一行来判断删除,
下面看下如何合并文本:
依旧是使用查询命令,组合替换命令来合并行
下面看下多行删除命令:
删除第一个空白行。
下面看下多行打印命令:
下面看下排序命令:
感叹号代表取反的意思
下面看下利用正则通配,替换字符:
下面我们看下,如何在原来的单词上,加上重点标记:
&符号,可以给本身的这个单词加上替换标记
下面看一些实战小例子:
加倍行间距
去掉末尾的空白行,加倍后:
格式化原来的行:
给文件加个行数:
比较难看,换一种方式:
只打印最后一行:
删除多余的空白行:
删除开头的空白行:
删除html标签:
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]$