p 打印匹配行
= 显示文件行号
a\ 在定位行之后附件新文本信息
i\ 在定位行之前附件新文本信息
d 删除定位行
c\ 用新文本替换定位文本
r 从另一个文本中读文本
w 写文本到另一个文件
[root@netposa13501206 ~]# cat 10.log
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Mis P.Neave was in attendance.
# 显示文本第2行
[root@netposa13501206 ~]# sed -n ‘2p‘ 10.log
It was an evening of splendid music and company.
# 打印1-3行
[root@netposa13501206 ~]# sed -n ‘1,3p‘ 10.log
The honeysuckle band played all night long for only 0.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
# 打印1行和3行
[root@netposa13501206 ~]# sed -n ‘1p;3p‘ 10.log
The honeysuckle band played all night long for only $90.
Too bad the disco floor fell through at 23:10.
# 打印第1行和其后的1行
[root@netposa13501206 ~]# sed -n ‘1,+1p‘ 10.log
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
# 打印配到Neave的行
[root@netposa13501206 ~]# sed -n ‘/Neave/p‘ 10.log
The local nurse Mis P.Neave was in attendance.
# 显示模式匹配到The的第4行
[root@netposa13501206 ~]# sed -n ‘4,/The/p‘ 10.log
The local nurse Mis P.Neave was in attendance.
# 匹配$
[root@netposa13501206 ~]# sed -n ‘/\$/p‘ 10.log
The honeysuckle band played all night long for only $90.
[root@netposa13501206 ~]# sed -n ‘1,$p‘ 10.log
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Mis P.Neave was in attendance.
# 匹配任意以ing结尾的单词所在的行
[root@netposa13501206 ~]# sed -n ‘/.*ing/p‘ 10.log
It was an evening of splendid music and company.
# 仅打印行号
[root@netposa13501206 ~]# sed -n ‘/music/=‘ 10.log
2
# 打印行号及内容
[root@netposa13501206 ~]# sed -n -e ‘/music/=‘ -e ‘/music/p‘ 10.log
2
It was an evening of splendid music and company.
# 末尾附件三行:123,1234,12345
[root@netposa13501206 ~]# sed ‘$a> 123> 1234> 12345‘ 10.log
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Mis P.Neave was in attendance.
123
1234
12345
# company所在行后插入字符串;第4行前插入字符串;honeysuckle所在行替换为字符串;最后一行插入两行119、120。执行后不改变原文本。
[root@netposa13501206 ~]# cat 123.sed
#!/bin/sed -f
/company/ aThen suddenly it hanppend.
4 iUtter confusion followed.
/honeysuckle/ cThe Office Dibble band played well.
$ a119120
[root@netposa13501206 ~]# chmod +x 123.sed
[root@netposa13501206 ~]# 123.sed 10.log
The Office Dibble band played well.
It was an evening of splendid music and company.
Then suddenly it hanppend.
Too bad the disco floor fell through at 23:10.
Utter confusion followed.
The local nurse Mis P.Neave was in attendance.
119
120
# 按行号删除
[root@netposa13501206 ~]# sed ‘1d‘ 10.log
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Mis P.Neave was in attendance.
[root@netposa13501206 ~]# sed ‘1d;4d‘ 10.log
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
# 按模式匹配删除
[root@netposa13501206 ~]# sed ‘/Neave/d‘ 10.log
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
# 将night替换为NIGHT
[root@netposa13501206 ~]# sed ‘s/night/NIGHT/‘ 10.log
# 将night全部替换为NIGHT
[root@netposa13501206 ~]# sed ‘s/night/NIGHT/g‘ 10.log
# 将night替换为NIGHT,并将结果输出到sed.out
[root@netposa13501206 ~]# sed ‘s/night/NIGHT/w sed.out‘ 10.log
[root@netposa13501206 ~]# cat sed.out
The honeysuckle band played all NIGHT long for only $90.
[root@netposa13501206 ~]# sed -n ‘s/nurse/MISS &/p‘ 10.log
The local MISS nurse Mis P.Neave was in attendance.
# 1-3行重定向到file1.out
[root@netposa13501206 ~]# sed ‘1,3 w file1.out‘ 10.log
[root@netposa13501206 ~]# cat file1.out
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
# 将night匹配的行重定向到file2.out
[root@netposa13501206 ~]# sed ‘/night/ w file2.out‘ 10.log
[root@netposa13501206 ~]# cat file2.out
The honeysuckle band played all night long for only $90.
[root@netposa13501206 ~]# cat sedex.txt
Boom boom went the music.
[root@netposa13501206 ~]# sed ‘/company./r sedex.txt‘ 10.log
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Boom boom went the music.
Too bad the disco floor fell through at 23:10.
The local nurse Mis P.Neave was in attendance.
# 将23:00改为00:00
[root@netposa13501206 ~]# time="23:10"
[root@netposa13501206 ~]# Time="00:00"
[root@netposa13501206 ~]# sed -n "s/$time/$Time/p" 10.log
Too bad the disco floor fell through at 00:00.
空格: [ ]
tab: \t
原文:https://www.cnblogs.com/ly447742/p/13992805.html