文本处理:sed命令

一、sed的意义

1.sed的作用
对文本进行逐行处理的行编辑器。
2.sed模式空间
默认不直接处理源文件,仅对模式空间中的数据做处理,处理结束后,将模式空间打印到屏幕。它不直接处理文件的文本本身,它是把需要处理的数据加载到内存当中,然后在内存中进行处理。然后这个内存空间叫做模式空间。

二、sed的用法总结

1.sed常用参数

符号意义
-e多条件编辑
-r支持拓展正则表达式
-n只显示匹配出的行
-f指定sed脚本
-i直接修改源文件
=显示文件行号

2.行号显示
使用行号,可以是一个简单的数字,也可以是一个行号范围。

符号意义
xx为行号
x,y表示行号从x到y
/pattern查询包含模式的行
/pattern/pattern查询包含两个模式的行
pattern/,x在给定行号上查询包含模式的行
x,/pattern/通过行号和模式查询匹配到的行
x,y!查询不包含指定行号x和y的行

3.示例1
(1)创建一个文件

[root@localhost ~]# vim test.txt
1
2
3
4
5
6
7
8
9
10

(2)打印文件中的第二行,默认会全部输出。加-n选项,只打印匹配行

[root@localhost ~]# sed '2p' test.txt
1
2
2
3
4
5
6
7
8
9
10
[root@localhost ~]# sed -n '2p' test.txt
2

(3)取反

[root@localhost ~]# sed -n '2!p' test.txt
1
3
4
5
6
7
8
9
10

(4)打印第一行到第三行

[root@localhost ~]# sed -n '1,3p' test.txt
1
2
3

4.示例2
对于文本的处理,无外乎增删改查。
首先创建一个文件:

[root@localhost ~]# vim sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line

5.删除命令
使用d命令可删除指定的行。
将文件的第一行删除后输出到屏幕

[root@localhost ~]# sed '1d' sed.txt
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line

sed默认不修改源文件,如果想要保存修改后的文件,可以重定向到新的文件或者直接使用"-i"参数修改。
删除指定范围的行(第一行到第三行)

[root@localhost ~]# sed '1,3d' sed.txt
this is line 4,this is Fourth line
this is line 5,this is Fifth line

删除指定行到下两行之间的内容,比如删除第三行到下两行之间的内容

[root@localhost ~]# sed '3,+2d' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

删除第三行到最后行

[root@localhost ~]# sed '3,$d' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

删除最后一行

[root@localhost ~]# sed '$d' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line

6.跨奇数、偶数删除
首先往文本里再增加一些数据

[root@localhost ~]# vim sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9
10

跨奇数删除

[root@localhost ~]# sed '1~2d' sed.txt
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
6
8
10

跨偶数删除

[root@localhost ~]# sed '2~2d' sed.txt
this is line 1,this is First line

this is line 5,this is Fifth line
7
9

7.定点删除
定点删除时,如果知道行数,直接用行数删除,不过大多数情况下都不知道行数,那么可以用下面的方法。
需要再向文本里加些内容:

[root@localhost ~]# vim sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

删除所有包含’Empty’的行

[root@localhost ~]# sed '/Empty/d' sed.txt
this is line 1,this is First line

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

删除所包含’Empty’到最后的行

[root@localhost ~]# sed '/Empty/,$d' sed.txt
this is line 1,this is First line

删除所包含’Empty’到下两行的行

[root@localhost ~]# sed '/Empty/,+2d' sed.txt
this is line 1,this is First line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

删除从’You jump!'到’I jump!'的行,关键在于查找关键词

[root@localhost ~]# sed '/^Y/,/^I/d' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
9
10

删除空行

[root@localhost ~]# sed '/^$/d' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

需要注意的是标识空行,和含有空是有区别的

[root@localhost ~]# sed '/[[:space:]]/d' sed.txt

6
7
8
9
10

8.查找替换
使用s命令可将查找到的匹配文本内容替换为新的文本,默认情况只替换第一次匹配到的内容。

[root@localhost ~]# sed 's/line/LINE/' sed.txt
this is LINE 1,this is First line
this is LINE 2,the Second line,Empty line followed

this is LINE 4,this is Fourth line
this is LINE 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

如果只替换第二个匹配到line为LINE

[root@localhost ~]# sed 's/line/LINE/2' sed.txt
this is line 1,this is First LINE
this is line 2,the Second LINE,Empty line followed

this is line 4,this is Fourth LINE
this is line 5,this is Fifth LINE
6
You jump!
7
8
I jump!
9
10

使用g选项可以完成所有匹配值得替换

[root@localhost ~]# sed 's/line/LINE/g' sed.txt
this is LINE 1,this is First LINE
this is LINE 2,the Second LINE,Empty LINE followed

this is LINE 4,this is Fourth LINE
this is LINE 5,this is Fifth LINE
6
You jump!
7
8
I jump!
9
10

将以this开头的this替换成that

[root@localhost ~]# sed 's/^this/that/' sed.txt
that is line 1,this is First line
that is line 2,the Second line,Empty line followed

that is line 4,this is Fourth line
that is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

将符号",“换成”:"

[root@localhost ~]# sed 's/,/:/' sed.txt
this is line 1:this is First line
this is line 2:the Second line,Empty line followed

this is line 4:this is Fourth line
this is line 5:this is Fifth line
6
You jump!
7
8
I jump!
9
10

删除每一行的第一个字符

[root@localhost ~]# sed 's/^.//' sed.txt
his is line 1,this is First line
his is line 2,the Second line,Empty line followed

his is line 4,this is Fourth line
his is line 5,this is Fifth line

ou jump!


 jump!

0

在匹配到Empty的行,行首的单词前添加you can see

[root@localhost ~]# sed '/Empty/s/^/you can see /' sed.txt
this is line 1,this is First line
you can see this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

9.字符转换
使用y命令可以进行字符转换,其作用为将一系列字符逐个变换为另一系列字符。
注意:其为一一对应关系,所以要求转换字符与被转换字符的长度相等。
基本用法如下:

[root@localhost ~]# sed 'y/1245/ABCD/' sed.txt
this is line A,this is First line
this is line B,the Second line,Empty line followed

this is line C,this is Fourth line
this is line D,this is Fifth line
6
You jump!
7
8
I jump!
9
A0

10.插入文本
使用i或a命令插入文本,其中i代表在匹配行之前插入,而a代表在匹配行之后插入。
使用i在第二行之前插入i love you

[root@localhost ~]# sed '2 i i love you' sed.txt
this is line 1,this is First line
i love you
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

使用a在第二行之后插入i love you too

[root@localhost ~]# sed '2 a i love you too' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
i love you too

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

在匹配行的上一行插入文本yes

[root@localhost ~]# sed '/Second/ i yes' sed.txt
this is line 1,this is First line
yes
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

如果想要增加多行,则每行之间要用反斜杠\n来进行新行的添加

[root@localhost ~]# sed '2 a i love you \ni love you too' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
i love you
i love you too

this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

11.取代行
可以使用c命令,命令后面可以接字符串,这些字符串可以取代n1,n2之间的行

[root@localhost ~]# sed '2,4 c this is 2-4 line' sed.txt
this is line 1,this is First line
this is 2-4 line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

12.读入文本
使用r命令可以从其他文件中读取文本,并插入匹配行之后。
例:将/etc/passwd中的内容读出放到文档空行之后

[root@localhost ~]# sed '/^$/ r /etc/passwd' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

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/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
nginx:x:997:995:nginx user:/var/cache/nginx:/sbin/nologin
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

13.打印
使用p命令可进行打印,这里使用sed命令时一般都加-n参数,表示不打印没关系的行
不加-n参数,会输出所有行,找到的行会重复显示。

[root@localhost ~]# sed -n '/the/ p' sed.txt
this is line 2,the Second line,Empty line followed

当用到sed不同的编辑命令时,可以用{},不同的编辑命令之间用分号隔开

[root@localhost ~]# sed -n '/the/ {=;p}' sed.txt
2
this is line 2,the Second line,Empty line followed

打印前五行

[root@localhost ~]# sed -n '1,5p' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line
[root@localhost ~]# sed '5q' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line
this is line 5,this is Fifth line

打印出匹配First的行到第四行

[root@localhost ~]# sed -n '/First/,4p' sed.txt
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Fourth line

打印出不包含line的行

[root@localhost ~]# sed -n '/line/!p' sed.txt

6
You jump!
7
8
I jump!
9
10

14.sed脚本
使用sed脚本可以加快工作效率,调用sed命令并使用-f参数指定文件。
写如下脚本,作用是将全文的this改成THAT,并删除所有空行;然后执行脚本。

[root@localhost ~]# cat > sed1.txt <<EOF
> s/this/THAT/g
> /^$/d
> EOF
[root@localhost ~]# sed -f sed1.txt sed.txt
THAT is line 1,THAT is First line
THAT is line 2,the Second line,Empty line followed
THAT is line 4,THAT is Fourth line
THAT is line 5,THAT is Fifth line
6
You jump!
7
8
I jump!
9
10

15.sed总结
sed默认不对源文件执行操作,如果想要生效,则使用-i,而且默认全部打印输出,如果只打印匹配到的行,用p,前面加上-n参数。

删除:d,定点删除,从哪里到哪里删除,删除开头,结尾等。跨奇数、偶数删除,包括单词的行删除。

查找替换:s///,s@@@,s###,s&&&等读好,都可以使用。

字符转换:y///,但需要注意的是字符长度必须相同,因为是一一对应。

插入文本:i insert:匹配行的前一行插入。a append:匹配行的后一行插入。如果想要添加多行,需要使用\n换行符。

取代行:选择几行到几行,然后加c,后面再加上用来替代的文字。

三、sed演示示例

sed中常用的符号解释:

符号意义
\u表示把第一个字符转化为大写字母。uppercase
\l表示小写
&表示匹配前面正则表达式的那部分
\b匹配一个单词边界,也就是指单词和空格间的位置

1.删除/etc/grub2.cfg文件中所有以空白开头的行,且如果想要删除这些空白字符应该怎么做?

[root@localhost ~]# sed '/^[[:space:]]/d' /etc/grub2.cfg
[root@localhost ~]# sed 's/^[[:space:]]//g' /etc/grub2.cfg

2.删除/etc/fstab文件中所有以#开头的行

[root@localhost ~]# sed '/^#/d' /etc/fstab

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=5be02c6a-f908-4692-82a9-72cc756a9a0d /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

3.将/etc/fstab中全部添加#,并以#开头。

[root@localhost ~]# sed 's/^/#/' /etc/fstab
#
##
## /etc/fstab
## Created by anaconda on Mon Jun 22 02:16:49 2020
##
## Accessible filesystems, by reference, are maintained under '/dev/disk'
## See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
##
#/dev/mapper/centos-root /                       xfs     defaults        0 0
#UUID=5be02c6a-f908-4692-82a9-72cc756a9a0d /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

4.删除/etc/passwd中的偶数行

[root@localhost ~]# sed '2~2d' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin

5.从/etc/passwd中找出包含root的行,并把root改成rooter

[root@localhost ~]# sed 's/root/rooter/g' /etc/passwd
[root@localhost ~]# sed -n 's/root/rooter/gp' /etc/passwd
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin

6.从/etc/passwd中找出以bash结尾并在这行最后面加上一句话:you are very good!

[root@localhost ~]# sed '/bash$/ s/$/ you are very good!/' /etc/passwd
root:x:0:0:root:/root:/bin/bash you are very good!
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

7.删除/etc/passwd中头三行信息

[root@localhost ~]# sed '1,3d' /etc/passwd
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

8.只打印/etc/passwd中第五行到第十行信息

[root@localhost ~]# sed -n '5,10p' /etc/passwd
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

9.删除/etc/passwd中包含’ftp’的行到包含’ntp’的行

[root@localhost ~]# sed '/ftp/,/ntp/d' /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

10.删除/etc/passwd中所有包含nologin的行

[root@localhost ~]# sed '/nologin/d' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

11.将/etc/passwd中所有的bin改成sbin

[root@localhost ~]# sed 's/bin/sbin/g' /etc/passwd

12.在/etc/passwd中用how are you取代包含usr的行

[root@localhost ~]# sed '/usr/c how are you' /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
how are you
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

13.在/etc/passwd中把包含的数字用*代替

[root@localhost ~]# sed 's/[[:digit:]]/*/g' /etc/passwd
root:x:*:*:root:/root:/bin/bash
bin:x:*:*:bin:/bin:/sbin/nologin
daemon:x:*:*:daemon:/sbin:/sbin/nologin
adm:x:*:*:adm:/var/adm:/sbin/nologin
lp:x:*:*:lp:/var/spool/lpd:/sbin/nologin
sync:x:*:*:sync:/sbin:/bin/sync
shutdown:x:*:*:shutdown:/sbin:/sbin/shutdown
halt:x:*:*:halt:/sbin:/sbin/halt
mail:x:*:**:mail:/var/spool/mail:/sbin/nologin
operator:x:**:*:operator:/root:/sbin/nologin
games:x:**:***:games:/usr/games:/sbin/nologin
ftp:x:**:**:FTP User:/var/ftp:/sbin/nologin
nobody:x:**:**:Nobody:/:/sbin/nologin
systemd-network:x:***:***:systemd Network Management:/:/sbin/nologin
dbus:x:**:**:System message bus:/:/sbin/nologin
polkitd:x:***:***:User for polkitd:/:/sbin/nologin
sshd:x:**:**:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:**:**::/var/spool/postfix:/sbin/nologin
chrony:x:***:***::/var/lib/chrony:/sbin/nologin
nginx:x:***:***:nginx user:/var/cache/nginx:/sbin/nologin

14.在/etc/passwd中把包含sync的行到包含sshd的行全部用come on代替

[root@localhost ~]# sed '/sync/,/sshd/c come on' /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
come on
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
nginx:x:997:995:nginx user:/var/cache/nginx:/sbin/nologin

15.在/etc/passwd中把每个单词的第一个小写字母变成大写

[root@localhost ~]# sed 's/\<[a-z]/\u&/' /etc/passwd
[root@localhost ~]# sed 's/\<[a-z]/\u&/g' /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/nologin
Systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
Dbus:x:81:81:System message bus:/:/sbin/nologin
Polkitd:x:999:998:User for polkitd:/:/sbin/nologin
Sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
Postfix:x:89:89::/var/spool/postfix:/sbin/nologin
Chrony:x:998:996::/var/lib/chrony:/sbin/nologin
Nginx:x:997:995:nginx user:/var/cache/nginx:/sbin/nologin

16.在/etc/passwd中匹配以字母a开头的行尾加上一句话:to be no.1

[root@localhost ~]# sed -r 's/(^a.*)/\1 to be no.1/' /etc/passwd
[root@localhost ~]# sed '/^a/ s/$/ to be no.1/' /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 to be no.1

17.在/etc/passwd中找出包含nobody的行打印出,并只打印匹配到的内容

[root@localhost ~]# sed -n '/nobody/{p;=}' /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
13

18.在/etc/passwd中只打印从包含gnome到最后的行

[root@localhost ~]# sed -n '/gnome/,$p' /etc/passwd

19.在/etc/passwd中同时打印以mail开头的行和以rpm开头的行

[root@localhost ~]# sed -n -e '/^mail/p' -e '/^rpm/p' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

20.在/etc/passwd中从第五行开始到结尾把冒号换成星号

[root@localhost ~]# sed '5,$ s/:/*/g' /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/nologin
systemd-network*x*192*192*systemd Network Management*/*/sbin/nologin
dbus*x*81*81*System message bus*/*/sbin/nologin
polkitd*x*999*998*User for polkitd*/*/sbin/nologin
sshd*x*74*74*Privilege-separated SSH*/var/empty/sshd*/sbin/nologin
postfix*x*89*89**/var/spool/postfix*/sbin/nologin
chrony*x*998*996**/var/lib/chrony*/sbin/nologin
nginx*x*997*995*nginx user*/var/cache/nginx*/sbin/nologin

21.在/etc/passwd中打印出前十行

[root@localhost ~]# sed '10q' /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

22.从/etc/fstab文件中,打印出不包含#的行

[root@localhost ~]# sed -n '/#/!p' /etc/fstab

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=5be02c6a-f908-4692-82a9-72cc756a9a0d /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

23.在/etc/passwd中将第八行到匹配到行首为gdm的行首第一个字母替换成星号

[root@localhost ~]# sed '8,/^gdm/ s/^./*/g' /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
*alt:x:7:0:halt:/sbin:/sbin/halt
*ail:x:8:12:mail:/var/spool/mail:/sbin/nologin
*perator:x:11:0:operator:/root:/sbin/nologin
*ames:x:12:100:games:/usr/games:/sbin/nologin
*tp:x:14:50:FTP User:/var/ftp:/sbin/nologin
*obody:x:99:99:Nobody:/:/sbin/nologin
*ystemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
*bus:x:81:81:System message bus:/:/sbin/nologin
*olkitd:x:999:998:User for polkitd:/:/sbin/nologin
*shd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
*ostfix:x:89:89::/var/spool/postfix:/sbin/nologin
*hrony:x:998:996::/var/lib/chrony:/sbin/nologin
*ginx:x:997:995:nginx user:/var/cache/nginx:/sbin/nologin

24.在/etc/passwd中在匹配以sshd开头的行的后面添加两行内容,分别为:are you ok? yes,i’m fine!

[root@localhost ~]# sed '/sshd/ a are you ok? \nyes,i'\''m fine!' /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/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
are you ok?
yes,i'm fine!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值