首页 > 其他 > 详细

二、sed

时间:2020-11-17 11:27:21      阅读:46      评论:0      收藏:0      [点我收藏+]

1、基本编辑命令

p      打印匹配行
=      显示文件行号
a\     在定位行之后附件新文本信息
i\     在定位行之前附件新文本信息
d      删除定位行
c\     用新文本替换定位文本
r      从另一个文本中读文本
w      写文本到另一个文件

2、使用示例

[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.1 使用p显示行

# 显示文本第2行
[root@netposa13501206 ~]# sed -n ‘2p‘ 10.log
It was an evening of splendid music and company.

2.2 打印范围

# 打印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.

2.3 打印模式匹配

# 打印配到Neave的行
[root@netposa13501206 ~]# sed -n ‘/Neave/p‘ 10.log
The local nurse Mis P.Neave was in attendance.

2.4 使用模式和行号进行匹配

# 显示模式匹配到The的第4行
[root@netposa13501206 ~]# sed -n ‘4,/The/p‘ 10.log
The local nurse Mis P.Neave was in attendance.

2.5 匹配元字符

# 匹配$
[root@netposa13501206 ~]# sed -n ‘/\$/p‘ 10.log
The honeysuckle band played all night long for only $90.

2.6 显示整个文件

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

2.7 任意字符

# 匹配任意以ing结尾的单词所在的行
[root@netposa13501206 ~]# sed -n ‘/.*ing/p‘ 10.log
It was an evening of splendid music and company.

2.8 打印行号

# 仅打印行号
[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.

2.9 附件文本

# 末尾附件三行: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

2.10 创建sed脚本文件

# 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

2.11 删除文本

# 按行号删除
[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.

2.12 替换文本

# 将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.

2.13 替换修改,保留原字符

[root@netposa13501206 ~]# sed -n ‘s/nurse/MISS &/p‘ 10.log
The local MISS nurse Mis P.Neave was in attendance.

2.14 w结果重定向

# 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.

2.15 添加文件,来自文本

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

3、其他补充

3.1 sed使用shell变量

# 将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.

3.2 空格、tab表示方式

空格:  [ ]
tab:    \t

二、sed

原文:https://www.cnblogs.com/ly447742/p/13992805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!