一、15个参数
1.. r 从文件读入
2.. w写入文件
3.. a 追加命令
4.. i 插入
5.. n 下一个
6.. y 变形命令
7.. q 退出命令
8.. h命令
9.. G命令
10.. x命令
11.. -n选项取消sed的默认行为
12.. 删除:d命令
13.. 替换:s命令
14.. 多点编辑:e命令
15.. -f 引导sed脚本文件名
11.. -n选项取消sed的默认行为
12.. 删除:d命令
13.. 替换:s命令
14.. 多点编辑:e命令
15.. -f 引导sed脚本文件名
16.. 选定行的范围:逗号
有兴趣可加入Q群讨论 298148856
二、利用sed 减少反复工作
三、sed中使用系统变量
四、小案例
1.对匹配进行追加和插入
一、15个参数
1.. r 从文件读入
[root@watchout2 ~]# cat file
1
2
3
4
5
1
2
3
4
5
[root@watchout2 ~]# cat newfile
a
b
c
d
e
a
b
c
d
e
[root@watchout2 ~]# sed '/a/r file' newfile (读入文件file,并显示在newfile文件中匹配行a之后)
a
1
2
3
4
5
b
c
d
e
[root@watchout2 ~]# touch /file
[root@watchout2 ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaa" > /file
[root@watchout2 ~]# sed '/a/r /file' newfile (读入的文件在不同的路径)
a
aaaaaaaaaaaaaaaaaaaaaaaaaa
b
c
d
e
[root@watchout2 ~]#
a
1
2
3
4
5
b
c
d
e
[root@watchout2 ~]# touch /file
[root@watchout2 ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaa" > /file
[root@watchout2 ~]# sed '/a/r /file' newfile (读入的文件在不同的路径)
a
aaaaaaaaaaaaaaaaaaaaaaaaaa
b
c
d
e
[root@watchout2 ~]#
2.. w写入文件
[root@watchout2 ~]# sed '/a/w bo' newfile
a
b
c
d
e
[root@watchout2 ~]# cat bo ([root@watchout2 ~]# sed -n '/a/w bobo' newfile )
a
a
b
c
d
e
[root@watchout2 ~]# cat bo ([root@watchout2 ~]# sed -n '/a/w bobo' newfile )
a
3.. a 追加命令
[root@watchout2 ~]# sed '/b/a \bobo' newfile
a
b
a
b
bobo
c
d
e
c
d
e
4.. i 插入
[root@watchout2 ~]# sed '/a/i \bobo' newfile
bobo
a
b
c
d
e
bobo
a
b
c
d
e
5.. n 下一个
[root@watchout2 ~]# sed -n '/a/{n;p}' newfile(打印匹配行的下一行)
b
b
[root@watchout2 ~]# sed '/a/{n;s/b/c/p}' newfile
a
c
c
c
d
e
a
c
c
c
d
e
6.. y 变形命令
[root@watchout2 ~]# sed '1,3y/abc/ABC/' newfile
A
B
C
d
e
A
B
C
d
e
y命令就是将小写转换成了大写,正则表达式元字符不能使用这个命令。
7.. q 退出命令
[root@watchout2 ~]# sed '3q' newfile
a
b
c
a
b
c
打印前三行后退出。
8.. h命令 是将pattern space 模式空间(临时缓冲区)的内容复制到holding buffer保持缓冲区
9.. G命令 是将holding buffer中的内容取得,尔后放回pattern space中,且追加到相应行的末 尾
g命令是将holding buffer 中的内容取得,尔后放回pattern space 中,且替换相应的行
[root@watchout2 ~]# sed -e '/a/h' -e '/d/G' newfile
a
b
c
d
a
e
a
b
c
d
a
e
h命令会把a匹配行,放入保持缓冲区,G命令会把保持缓冲区中的内容放入模式空间,并追加到匹配行d的下一行。
[root@watchout2 ~]# sed -e '/a/h' -e '$G' newfile
a
b
c
d
e
a
a
b
c
d
e
a
与上相同,只是$代表最后一行。
[root@watchout2 ~]# sed -e '/a/h' -e '/b/g' newfile
a
a
c
d
e
[root@watchout2 ~]# sed -e '/a/h' -e '$g' newfile
a
b
c
d
a
a
a
c
d
e
[root@watchout2 ~]# sed -e '/a/h' -e '$g' newfile
a
b
c
d
a
以上h命令会把匹配行a,放入保持缓冲区,g命令会读取保持缓冲区的内容,将匹配行b(第二个例子就是$最后一行)替换为a 。注:a将覆盖匹配行b(第二个例子就是$最后一行)
10.. x命令 是pattern space模式空间将被holding buffer保持缓冲区中的内容替换
[root@watchout2 ~]# sed -e '/a/h' -e '/d/x' newfile
a
b
c
a
e
a
b
c
a
e
匹配行d 将被匹配行a替换。
11.. -n选项取消sed的默认行为,sed 默认行为是-p ,
root:/tmp>sed '/6/p' num -p参数打印num的内容,尔后匹配“6”在次打印6,所以6会出现两次。
1
2
3
4
5
6
6
7
8
root:/tmp>sed -n '/6/p' num -n选项取消sed的默认行为(-p ),所以只打印“6”
6
root:/tmp>
1
2
3
4
5
6
6
7
8
root:/tmp>sed -n '/6/p' num -n选项取消sed的默认行为(-p ),所以只打印“6”
6
root:/tmp>
12.. 删除:d命令
删除第6行
root:/tmp>sed '6d' num
1
2
3
4
5
7
8
root:/tmp>
1
2
3
4
5
7
8
root:/tmp>
从第6行删除到行尾
root:/tmp>sed '6,$d' num
1
2
3
4
5
root:/tmp>
1
2
3
4
5
root:/tmp>
d删除最后一行
root:/tmp>sed '$d' num
1
2
3
4
5
6
7
root:/tmp>
1
2
3
4
5
6
7
root:/tmp>
d删除匹配行
root:/tmp>sed '/6/d' num
1
2
3
4
5
7
8
root:/tmp>
1
2
3
4
5
7
8
root:/tmp>
13.. 替换:s命令
s表示替换,g表示作用范围整个行,如果没有g标志则只有每行第一个被替换
root:/tmp>sed 's/1/8/g' num (sed默认有-p参数打印搜索后的所有行。如g后面加p,那么匹配行就会打印两次)
8
2
3
4
5
6
7
8
root:/tmp>
8
2
3
4
5
6
7
8
root:/tmp>
行首“1”开头被替换为“8”
root:/tmp>sed 's/^1/8/g' num
8
2
3
4
5
6
7
8
root:/tmp>
8
2
3
4
5
6
7
8
root:/tmp>
&符号表示替换字符串中被找到的部分,所以每个数字后面被追加.6789
root:/tmp>sed 's/[0-9]/&.6789/g' num
1.6789
2.6789
3.6789
4.6789
5.6789
6.6789
7.6789
8.6789
root:/tmp>
1.6789
2.6789
3.6789
4.6789
5.6789
6.6789
7.6789
8.6789
root:/tmp>
\(..\) 保存匹配的字符到标签\1中。 s/
\(5\)/
\1.6789/ 蓝色部分保存到标签\1中。从表达式最左边开始,向右最多可以使用9个标签
root:/tmp>sed 's/\(5\)/\1.6789/g' num
1
2
3
4
5.6789
6
7
8
root:/tmp>sed 's/\([0-9]\)/\1.6789/g' num
1.6789
2.6789
3.6789
4.6789
5.6789
6.6789
7.6789
8.6789
root:/tmp>
1
2
3
4
5.6789
6
7
8
root:/tmp>sed 's/\([0-9]\)/\1.6789/g' num
1.6789
2.6789
3.6789
4.6789
5.6789
6.6789
7.6789
8.6789
root:/tmp>
s后面的字符是分隔搜索字符串和替换字符串的分隔符。默认分隔符是斜杠,不论什么字符紧跟s命令都被认为是新的分隔符
root:/tmp>sed 's#6#shell#g' num
1
2
3
4
5
shell
7
8
root:/tmp>
1
2
3
4
5
shell
7
8
root:/tmp>
14.. 多点编辑:e命令
root:/tmp>sed -e '1,6d' -e 's/8/8.shell/g' num
7
8.shell
root:/tmp>
7
8.shell
root:/tmp>
15.. -f 引导sed脚本文件名
16.. 选定行的范围:逗号
从第一行到第五行
root:/tmp>sed -n '1,5p' num
1
2
3
4
5
1
2
3
4
5
从第一行到第五行,然后在搜索
root:/tmp>sed '1,5s/3/3.linux/g' num
1
2
3.linux
4
5
6
7
8
root:/tmp>sed '1,5s/3/3.linux/g' num
1
2
3.linux
4
5
6
7
8
显示从第三行到行首为6的中间所有行
root:/tmp>sed -n '3,/^6/p' num
3
4
5
6
root:/tmp>
root:/tmp>sed -n '3,/^6/p' num
3
4
5
6
root:/tmp>
匹配1和5后面追加“******Total********”
root:/tmp>sed '/1/,/5/s/$/******Total********/g' num
1******Total********
2******Total********
3******Total********
4******Total********
5******Total********
6
7
8
root:/tmp>
1******Total********
2******Total********
3******Total********
4******Total********
5******Total********
6
7
8
root:/tmp>
由于工作的需要我必须每次打开一个文件在300行到350,都需要将其注释,尔后在有需求时,在将注释去掉。所以反复工作效率非常低,请各位帮忙如何写一个脚本
2..1 举例说明:
数据文件nu
[root@watchout2 ~]# vi nu
1 ab
2 ab
3 ab
4
5
6
7
8
9
...
...
...
300
...
...
...
350
[root@watchout2 ~]# vi nu
1 ab
2 ab
3 ab
4
5
6
7
8
9
...
...
...
300
...
...
...
350
找到一个这样的脚本
sed -n '/ab/s/$/******AGEV******/p' nu
是在匹配行末尾追加******AGEV******
即:
[root@watchout2 ~]# sed -n '/ab/s/$/******AGEV******/p' nu
1 ab******AGEV******
2 ab******AGEV******
3 ab******AGEV******
sed -n '/ab/s/$/******AGEV******/p' nu
是在匹配行末尾追加******AGEV******
即:
[root@watchout2 ~]# sed -n '/ab/s/$/******AGEV******/p' nu
1 ab******AGEV******
2 ab******AGEV******
3 ab******AGEV******
根据以上需求将更改如下:
[root@watchout2 ~]# sed -n '/ab/s/^/#/gp' nu
#1 ab
#2 ab
#3 ab
-n 取消默认的-p选项
s进行搜索
^是在行首用#来替换
g是进行全面的行内替换
-n和p是同时使用是对匹配行进行打印
[root@watchout2 ~]# sed -n '/ab/s/^/#/gp' nu
#1 ab
#2 ab
#3 ab
-n 取消默认的-p选项
s进行搜索
^是在行首用#来替换
g是进行全面的行内替换
-n和p是同时使用是对匹配行进行打印
[root@watchout2 ~]# sed -n '1,3s/^/#/p' nu
#1 ab
#2 ab
#3 ab
#1 ab
#2 ab
#3 ab
2..2 解决问题
[root@watchout2 ~]# sed -n '300,350s/^/#/p' nu > newfile
尔后打开newfile文件
复制文件中的内容,把原文件nu中的300-350行删除,将复制内容粘贴。
复制文件中的内容,把原文件nu中的300-350行删除,将复制内容粘贴。
注:例子
在匹配的某一行添加注释
sed -i.0911-1352.bak '/\/fgn\/shell\/trace.sh/ s/^/#/' /tmp/root
如果有
在匹配的某一行去掉注释
sed -i.0911-1352.bak '/\/fgn\/shell\/trace.sh/ s/^#//' /tmp/root
将4到7行进行注释(后面可重新定向到一个文件)
[root@watchout2 ~]# sed '4,7s/^/#/' nu
1 ab
2 ab
3 ab
#4
#5
#6
#7
1 ab
2 ab
3 ab
#4
#5
#6
#7
将4到7行注释去掉(后面可重新定向到一个文件)
[root@watchout2 ~]# sed '4,7s/#//' nu
1 ab
2 ab
3 ab
4
5
6
7
[root@watchout2 ~]#
[root@watchout2 ~]# sed '4,7s/#//' nu
1 ab
2 ab
3 ab
4
5
6
7
[root@watchout2 ~]#
2..3 存在问题
个人感觉还是比较麻烦,请高手指点如何写成一个脚本,只要执行脚本就可达到原文件nu中的300行至350行,行首就增加注释“#”。
执行另一个脚本即可将300行至350行的注释去掉。
恭候您的赐教!
个人感觉还是比较麻烦,请高手指点如何写成一个脚本,只要执行脚本就可达到原文件nu中的300行至350行,行首就增加注释“#”。
执行另一个脚本即可将300行至350行的注释去掉。
恭候您的赐教!