Shell系统编程三剑客之----sed编辑器

目录

一:sed编辑器

1.sed编辑器概述

2.sed的工作流程

3.sed的命令格式

4.常用选项

5.常用操作

二:sed操作事例

1.查询

(1)打印内容

​(2)打印行数

​(3)打印特殊字符、ASCII码 

 (4)打印指定行数

​(5)配合正则表达式查询打印

2.删除

(1)删除行

(2)指定删除数

 3.替换

(1)4种替换标记

 (2)-f  :调用文件

 (3)w 替换到指定文件中

 (4)c :整行替换

(5)y :转换

4.插入

(1)a:行下插入

(2)i:行上插入

​(3)r :读取、合并

 (4)H :   复制    G :  粘贴

(5)剪切

(6)位置调换


一:sed编辑器

1.sed编辑器概述

  • sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。
  • sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。

2.sed的工作流程

sed的工作流程主要包括读取、执行和显示三个过程:
读取:sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间, pattern space)
执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。
显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行直至所有内容被处理完。
在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
注意:默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

3.sed的命令格式

格式1:sed  -e  ‘操作’  文件1 文件2 ......

格式2:sed  -n -e  '操作'   文件1  文件2 .......

格式3:sed  -f  脚本文件   文件1  文件2 .......

格式4:sed  -i  -e  '操作'  文件1  文件2.......

格式5:
sed  -e  ' n {
操作1 
操作2 
.......
}'  文件1  文件2......

4.常用选项

选项      解释
-e 或-expression= 示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令时使用
-f 或–file= 表示用指定的脚本文件来处理输入的文本文件
-h 或–help 显示帮助
-n、–quiet 或 silent 禁止sed编辑器输出,但可以与p命令一起使用完成输出
-i 直接修改目标文本文件
-r 表示支持正则表达式

5.常用操作

操作 解释
s 替换,替换指定字符
d 删除,删除选定的行
a 增加,在当前行下面增加一行指定内容
i 插入,在选定行上面插入一行指定内容
c 替换,将选定行替换为指定内容
y 字符转换,转换前后大的字符长度必须相同
p 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用
= 打印行号
l (小写L) 打印数据流中的文本和不可打印的ASCII字符(比如结束符$,制表符\t)

二:sed操作事例

1.查询

(1)打印内容

#-n和-p一起使用表示打印一次内容,如果不加-n,则打印两次内容
[root@localhost ~]# sed -n -e 'p' a
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

 (2)打印行数

[root@localhost ~]# cat a | sed -n '='
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# cat a | sed -n '=' |tail -1
12
[root@localhost ~]# cat a | wc -l
12

[root@localhost ~]#sed -e '=' testfile1       #如果不加 -n 的话就是既打印出了行号也打印出了内容
[root@localhost ~]#sed -n '=;p' testfile1     #结果和上面一样,既打印行号也打印内容
[root@localhost ~]#sed -n -e '=' -e 'p' testfile1     #结果和上面一样,表示先打印行号再打印内容
[root@localhost ~]# cat a | sed -n -e '=' -e 'p'
1
one
2
two
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine
10
ten
11
eleven
12

 (3)打印特殊字符、ASCII码 

#加 -l 是打印ASCII字符
[root@localhost ~]# cat a | sed -n 'l'
one$
two$
three$
four$
five$
six$
seven$
eight$
nine$
ten$
eleven$
twelve$

 (4)打印指定行数

[root@localhost ~]# sed -n '4p' a           #打印第4行
four
[root@localhost ~]# sed -n '$p' a           #打印最后一行,$为最后一行
twelve
[root@localhost ~]# sed -n '1,9p' a         #打印1-9行
one
two
three
four
five
six
seven
eight
nine
[root@localhost ~]# sed -n '1,+5p' a        #打印1+5行
one
two
three
four
five
six
[root@localhost ~]# sed '6q' a              #打印6行
one
two
three
four
five
six
[root@localhost ~]# sed -n 'p;n' a           #打印奇数行
one
three
five
seven
nine
eleven
[root@localhost ~]# sed -n 'n;p' a            #打印偶数行
two
four
six
eight
ten
twelve
[root@localhost ~]# sed -n '1 {p;n;n;p}' a    #从第1行开始,n为自动调下一行
one
three
[root@localhost ~]# sed -n '6,${n;p}' a       #从第6行开始,n为自动调下一行,到最后一行
seven
nine
eleven

  (5)配合正则表达式查询打印

[root@localhost ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sed -n '/^root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# sed -n '/bash$/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
qhw:x:1000:1000:qhw:/home/qhw:/bin/bash
[root@localhost ~]# sed -nr '/root|ftp/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost ~]# sed -n '2,/ftp/p' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost ~]# sed -n '2,/ftp/=' /etc/passwd
2
3
4
5
6
7
8
9
10
11
12

2.删除

(1)删除行

[root@192 ~]# sed '3d' a                   #删除第三行
one
two
four
five
six
seven
eight
nine
ten
eleven
twelve
[root@192 ~]# sed '3,5d' a               #删除3-5行
one
two
six
seven
eight
nine
ten
eleven
twelve
[root@192 ~]# sed 'd' a                  #文件内容全删

(2)指定删除数

[root@192 ~]# sed '/bash$/!d' /etc/passwd            #删除不以bash结尾的行
root:x:0:0:root:/root:/bin/bash
qhw:x:1000:1000:qhw:/home/qhw:/bin/bash

[root@192 ~]# sed -r '/^(bash)$/d' /etc/passwd       #删除不以bash结尾的行
root:x:0:0:root:/root:/bin/bash

[root@192 ~]# cat aa | sed '/3/d'                        #删除3
1
2
10
15
55
110
120
255
555

[root@192 ~]# cat aa | sed -r '/2|3/d'                   #删除2或3,循环删
1
10
15
55
110
555

[root@192 ~]# cat aa | sed '/2/,/20/d'                   #当没有那么多行时,会删除的就剩一行
1

 3.替换

(1)4种替换标记

数字 表明新字符串将替换第几处匹配的地方
g 表明新字符串将会替换所有匹配的地方
p 打印与替换命令匹配的行,与-n一起使用
w 文件 将替换的结果写到文件中
#将第一个root替换成admin
[root@192 ~]# sed -n 's/root/admin/p' /etc/passwd 
admin:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin

#将第二个root替换成admin
[root@192 ~]# sed -n 's/root/admin/2p' /etc/passwd 
root:x:0:0:admin:/root:/bin/bash

#将root都替换成admin
[root@192 ~]# sed -n 's/root/admin/gp' /etc/passwd 
admin:x:0:0:admin:/admin:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin

#将分割符全部替换为空
[root@192 ~]# sed -n 's/root//gp' /etc/passwd 
:x:0:0::/:/bin/bash
operator:x:11:0:operator:/:/sbin/nologin

#将分割符替换为空格
[root@192 ~]# sed -n 's/root/ /gp' /etc/passwd 
 :x:0:0: :/ :/bin/bash
operator:x:11:0:operator:/ :/sbin/nologin

#将文件内1-20行前面加#号
[root@192 ~]# sed -n '1,20 s/^/#/p' /etc/passwd

##表示将匹配到的整行文本以#开头进行注释,&表示匹配到的整行文本
[root@192 ~]#  sed -rn 's/.*root.*/#&/p' /etc/passwd
#root:x:0:0:root:/root:/bin/bash
#operator:x:11:0:operator:/root:/sbin/nologin

 (2)-f  :调用文件

sed -f c.sh a.txt    #对a.txt调用c.sh的规则

 (3)w 替换到指定文件中

#将前20行结果替换到指定文件中
[root@192 ~]# sed '1,20w d.txt' /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologi

 (4)c :整行替换

#将第一行替换为996
[root@192 ~]# sed '1c 996' a
996
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

#将最后一行替换为hello world
[root@192 ~]# sed '$c hello world' a
one
two
three
four
five
six
seven
eight
nine
ten
eleven
hello world

(5)y :转换

#将文件中o换为1,u换为2,r换为3
[root@192 ~]# sed 'y/our/123/' a
1ne
tw1
th3ee
f123
five
six
seven
eight
nine
ten
eleven
twelve

4.插入

(1)a:行下插入

#第一行下插入123
[root@192 ~]# sed '1a 123' a
one
123
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

#1-6行下插入123
[root@192 ~]# sed '1,6a 123' a
one
123
two
123
three
123
four
123
five
123
six
123
seven
eight
nine
ten
eleven
twelve

 

(2)i:行上插入

#第一行上插入123
[root@192 ~]# sed '1i 123' a
123
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

#最后一行上插入123
[root@192 ~]# sed '$i 123' a
one
two
three
four
five
six
seven
eight
nine
ten
eleven
123
twelve

 (3)r :读取、合并

#在a文件的最后一行后将aa文件读取结果显示出来
[root@192 ~]# sed '$r aa' a
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
1
2
3
10
15
55
110
120
255
555
555
255
333
333

 (4)H :   复制    G :  粘贴

#1-3行复制,粘贴到最后一行
[root@192 ~]# sed '1,3H;$G' a
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

one
two
three

#1-3行复制,粘贴到10到最后一行后面
[root@192 ~]# sed '1,3H;10,$G' a
one
two
three
four
five
six
seven
eight
nine
ten

one
two
three
eleven

one
two
three
twelve

one
two
three

(5)剪切

#1-3行复制,剪切到最后一行
[root@192 ~]# sed '1,3{H;d};$G' a
four
five
six
seven
eight
nine
ten
eleven
twelve

one
two
three

#1-3行复制,剪切到10-最后一行
[root@192 ~]# sed '1,3{H;d};10,$G' a
four
five
six
seven
eight
nine
ten

one
two
three
eleven

one
two
three
twelve

one
two
three

 

(6)位置调换

[root@localhost ~]# echo '112233' 
112233

#分组调换位置
[root@localhost ~]#  echo '112233' | sed  -r 's/(11)(22)(33)/\3\2\1/'
332211

[root@localhost ~]#  echo '112233' 
112233

#第一位和最后一位调换
[root@localhost ~]#  echo '112233' | sed  -r 's/(.)(.*)(.)/\3\2\1/'
312231

猜你喜欢

转载自blog.csdn.net/A1100886/article/details/130642703