sed进阶
改变流
对数据流中的特定行执行操作,基于地址、地址模式或地址区间排除一整块命令,当匹配后跳到标签指定位置执行命令
分支(branch)命令b,格式
[address]b [label] :label
标签最长7个字符,在没有指定标签的情况下,会直接跳到脚本结尾
]# sed '{/dat1/b jump;s/this is the/No jump on/
> :jump
> s/this is the/jump here on/}' data1.txt
No jump on header line
jump here on dat1 Lion
king is the dat2 line
this is Lion king line
No jump on dat4 line
this is Lion king line
]# echo "this, is, a, test, to, remove, commas."|sed -n '{
:start
s/,//1p
/,/b start
}'
this is, a, test, to, remove, commas.
this is a, test, to, remove, commas.
this is a test, to, remove, commas.
this is a test to, remove, commas.
this is a test to remove, commas.
this is a test to remove commas.
测试
测试(test)命令t用来改变sed脚本执行流程,测试命令会根据替换命令的结果跳到某个标签,而不是根据地址进行跳转,替换命令执行成功跳转,否则不跳转
[address]t [label]
如果未指明标签,则会直接跳到脚本结尾
> s/dat1/matched/
> t
> s/dat/NOMATCHED/
> }' data1.txt
this is the header line
this is the matched Lion
king is the NOMATCHED2 line
this is Lion king line
this is the NOMATCHED4 line
this is Lion king line
]# echo "this, is, a, test, to, remove, commas."|sed -n '{
:start
s/,//1p
t start
}'
this is, a, test, to, remove, commas.
this is a, test, to, remove, commas.
this is a test, to, remove, commas.
this is a test to, remove, commas.
this is a test to remove, commas.
this is a test to remove commas.
替代模式
主要用于提取匹配的文本以供使用
&符号
&
用来替代匹配模式中匹配的文本
示例,想要给匹配文本加""
]# echo "the cat sleep in his hat."|sed 's/.at/"&"/g'
the "cat" sleep in his "hat".
替换单独的单词
(pattern)表明子模式,\1第一个子模式分配符,\2第二个子模式分配符,依次类推
]# echo "the cat sleep in his hat."|sed 's/\(.at\) sleep/\1/g'
the cat in his hat.
]# echo "1234567"|sed '{
> :start
> s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
> t start
> }'
1,234,567
]# echo "1234567"|sed '{
:start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/1p
t start
}'
1234,567
1,234,567
1,234,567
在脚本中使用sed
在脚本中用$()将sed输出重定向到变量
创建sed实用工具
1.加倍行间距
]# sed 'G' data1.txt
this is the header line
this is the dat1 Lion
最后一行不加空行
]# sed '$!G' data1.txt
this is the header line
this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line
对可能含有空白行的文本,加倍行间距
]# sed '/^$/d;$!G' data1.txt
this is the header line
this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line
加行号显示
]#sed '=' data1.txt |sed 'N;s/\n/ /'
1
2 this is the header line
3 this is the dat1 Lion
4 king is the dat2 line
同样也具备此功能:
nl data1.txt
cat -n data1.txt
打印末尾行
sed -n '$p' data1.txt
this is Lion king line
打印最后m行
]# sed '{
:start
$q; N; 3,$D
b start
}' data1.txt
this is the dat4 line
this is Lion king line
等于tail -2 data1.txt
删除行
删除连续空白行
]# cat data3.txt
this is the header line
this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line
]# sed '/./,/^$/!d' data3.txt
this is the header line
this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line
模式匹配第一行至少包含一个字符,紧接着匹配空行,如果匹配则不删除,其他删除
删除开头空白行
]# sed '/./,$!d' data3.txt
this is the header line
this is the dat1 Lion
king is the dat2 line
删除结尾空白行
]# sed '{
:start
/^\n*$/{$d;N;b start}
}' data3.txt
this is the header line
this is the dat1 Lion
king is the dat2 line
this is Lion king line
this is the dat4 line
this is Lion king line
删除HTML标签
]# cat >> data11.txt << EOF
> <html>
> <head>
> <title>this is title</title>
> <body>
> <p>
> this is the <b>first</b> line in the web page
> this second page.
> </body>
> EOF
[root@localhost day9]# sed 's/<.*>//g;/^$/d' data11.txt
this is the line in the web page
this second page.
[root@localhost day9]# sed 's/<[^>]*>//g;/^$/d' data11.txt
this is title
this is the first line in the web page
this second page.
删除不能含有>的行,例如title行,默认匹配则会全部删除