一sed介绍

三剑客是grep,sed,awk,功能都很强大。

其中sed是Stream EDitor,流编辑器 行,编辑器的简写,它一次处理一行内容。

sed的强大在于可以对文件进行修改,很适合在脚本中使用,就不需要在vim中修改文件。

处理时把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace),

接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。

然后读入下行,执行下一个循环。如果没有使诸如‘D’的特殊命令,那会在两个循环之间清空模式空间,但不会清空保留空间。

这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。

功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

官方参考文档:http://www.gnu.org/software/sed/manual/sed.html

默认是基于GNU项目开发出来的软件

二sed的工作原理

sed会自动在内存开辟一块空间给自己使用,这块内存空间叫做模式空间。

一个文件里面有很多行,sed每次读取文件中的一行,读取到自己的内存空间,还可以根据sed指令对一行行进行切割。

处理完了一行,这行就没有了,接着读取下面的行内容。

上面的处理是全自动的,相当于其内置了循环。

三sed工具的使用

(一)用法

sed   [option]...    'script'    inputfile...

注意脚本不是shell脚本,而是sed自带的语言。

脚本的格式由两部分组合   script:'地址 命令'

地址就是文件的行,读入一行要看是否符合地址定义的行。

最简单的就是不写地址,只有命令。

(二)常用选项

-n:不输出模式空间内容到屏幕,即不自动打印

n是silent的简写

-n, --quiet, --silent

suppress automatic printing of pattern space

-e: 多点编辑

-e script, --expression=script

add the script to the commands to be executed
指定pattern 和对应的 command

-f:/ PATH/SCRIPT_FILE : 从指定文件中读取编辑脚本

-f script-file, --file=script-file

add the contents of script-file to the commands to be executed

-r: 支持使用扩展正则表达式

-r, --regexp-extended

use extended regular expressions in the script.

-i.bak: 备份文件并原处编辑

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if SUFFIX supplied)

(三)编辑命令

查看帮助文档

d: 删除模式空间匹配的行,并立即启用下一轮循环

d      Delete pattern space.  Start next cycle.

p:打印当前模式空间内容,追加到默认输出之后

p Print the current pattern space.

P Print up to the first embedded newline of the current pattern space.

a [\]text:在指定行后面追加文本支持使用\n实现多行追加

a \  text   Append text, which has each embedded newline preceded by a backslash.

i [\]text:在行前面插入文本

i \   text   Insert text, which has each embedded newline preceded by a backslash.

c [\]text:替换行为单行或多行文本

c \

text   Replace the selected lines with text, which has each embedded newline preceded by a backslash.

w /path/somefile: 保存模式匹配的行至指定文件

w filename
              Write the current pattern space to filename.

r /path/somefile:读取指定文件的文本至模式空间中匹配到的行后

r filename
              Append text read from filename.

=: 为模式空间中的行打印行号

=      Print the current line number.

!:模式空间中匹配行取反处理

(四)脚本格式介绍

脚本是由地址和命令组合成的。

地址就是文件的行,读入一行要看是否符合地址定义的行。最简单是不写地址,只有命令。

1地址定界

(1) 不给地址:对全文进行处理,地址就相当于过滤条件。

就像你是人,指的是全地球人类的一员。

1)默认就会自动打印,所以是打印两遍。

[root@centos72 ~]# sed   'p'   /etc/issue
\S
\S
Kernel \r on an \m
Kernel \r on an \m

不加引号也可以

[root@centos65 ~]# sed   p   /etc/issue
CentOS release 6.8 (Final)
CentOS release 6.8 (Final)
Kernel \r on an \m
Kernel \r on an \m

2)自动打印关闭

把自动打印关闭,现在就打印1遍。

注意-n只要不加在命令前面即可。

-n, --quiet, --silent

suppress automatic printing of pattern space

p Print the current pattern space.

P Print up to the first embedded newline of the current pattern space.

[root@centos72 ~]# sed   'p'   /etc/issue  -n
\S
Kernel \r on an \m [root@centos72 ~]# sed -n 'p' /etc/issue
\S
Kernel \r on an \m

上面单纯的打印没有什么实际意义,因为cat也可以实现

[root@centos72 ~]# cat  /etc/issue
\S
Kernel \r on an \m

(2)单地址:
#: 指定的行,$:最后一行,/pattern/:被此处模式所能够匹配到的每一行

1)#=2表示文件的第2行打印

[root@centos72 ~]# sed      '2p'   /etc/issue
\S
Kernel \r on an \m
Kernel \r on an \m [root@centos72 ~]# sed -n '2p' /etc/issue
Kernel \r on an \m

2)地址为空并且自动打印,那么就是把文件内容全部打印一遍

[root@centos72 ~]# sed  ''  /etc/issue
\S
Kernel \r on an \m

3)使用sed读取键盘输入的内容

因为把自动打印关了,只对第2行打印了1遍

[root@centos72 ~]# sed  -n  '2p'
aaaaa
bbbbb
#输入的内容
bbbbb
#自动打印的
ccccc ddddd

4)

sed默认是支持标准输入,可以使用管道传输,适合显示命令结果的第几行。

这种方法可以取代head,tail。很多以前麻烦的事,sed都可以解决的。

方法多了就可以找到和使用更好的方法,这和学数学一样,解题方法不只一种,发散思维的前提是对知识的积累和掌握。

显示网络接口信息

[root@centos72 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.137.72 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::b029:2522:876f:5456 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fc:69:f8 txqueuelen 1000 (Ethernet)
RX packets 1889 bytes 178723 (174.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1118 bytes 173152 (169.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

打印标准输入网络接口信息的第2行内容

[root@centos72 ~]# ifconfig   |  sed    -n  '2p'
inet 192.168.137.72 netmask 255.255.255.0 broadcast 192.168.137.255

5)

$:最后一行

打印文件最后一行的信息

[root@centos72 ~]# sed  ''  /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Jan 13 00:14:21 2019
#
# 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
#
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@centos72 ~]# sed -n '$p' /etc/fstab
/dev/cdrom /mnt iso9660 defaults 0 0

6)

/pattern/:被此处模式所能够匹配到的每一行,模式支持正则表达式

打印文件以U开头的所有行

[root@centos72 ~]#  sed   -n    '/^U/p'    /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0

/pattern/前后不加引号也可以

[root@centos72 ~]#  sed   -n    /^U/p    /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0

注意地址后面一定要加上命令,否则会报错

[root@centos72 ~]# sed   '/^U/'    /etc/fstab
sed: -e expression #1, char 4: missing command

grep更方便

[root@centos72 ~]#  grep   ^U  /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0
[root@centos72 ~]# grep '^U' /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0

(3)地址范围,也就是第几行到第几行
#,#
#,+#
/pat1/,/pat2/
#,/pat1/

1)

#,#

nl   /etc/fstab 显示此文件的所有行号

[root@centos72 ~]# nl  /etc/fstab 

     1    #
2 # /etc/fstab
3 # Created by anaconda on Sun Jan 13 00:14:21 2019
4 #
5 # Accessible filesystems, by reference, are maintained under '/dev/disk'
6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 #
8 UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
9 UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
10 UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
11 UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0
12 /dev/cdrom /mnt iso9660 defaults 0 0
[root@centos72 ~]# sed -n '8,12p' /etc/fstab
#
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0

sed的行号是包括空行的

使用cat -n打印行号更准确。

[root@centos72 ~]# cat  -n  /etc/fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Sun Jan 13 00:14:21 2019
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
10 UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
11 UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
12 UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0
13 /dev/cdrom /mnt iso9660 defaults 0 0
[root@centos72 ~]# sed  -n  '9,13p'   /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@centos72 ~]# sed -n '9,12p' /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0

2)

#,+#  从第几行到本行之后的第几行

[root@centos72 ~]# cat  -n  /etc/fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Sun Jan 13 00:14:21 2019
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
10 UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
11 UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
12 UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0
13 /dev/cdrom /mnt iso9660 defaults 0 0
[root@centos72 ~]# sed -n '9,+3p' /etc/fstab
UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 / xfs defaults 0 0
UUID=ac6bb7e3-fa78-4eb2-b00d-e85c421c1bb0 /app xfs defaults 0 0
UUID=92886c3f-42a3-40f4-8cf7-c6890ca3a52e /boot xfs defaults 0 0
UUID=104520e1-0e97-4248-8fd0-a21e7d88a881 swap swap defaults 0 0

注意只能+,不能-

[root@centos72 ~]# sed  -n  '9,-3p'   /etc/fstab
sed: -e expression #1, char 3: unexpected `,'

3)/pat1/,/pat2/  打印两个模式之间的内容,也就要匹配两个条件

[root@centos72 ~]# sed  ''    /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
wang:x:1000:1000:wang:/home/wang:/bin/bash

b开头h结尾的行

[root@centos72 ~]# sed  -n   /^b/,/^h/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
[root@centos72 ~]# sed -n '/^b/,/^h/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

行以r开头u结尾,但是没有u结尾就打印到文件的最后一行

[root@centos72 ~]# cat  /etc/passwd   |  sed  -n   '/^r/,/^u/p'
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
wang:x:1000:1000:wang:/home/wang:/bin/bash

4)#,/pat1/

从第几行到匹配模式的行

[root@centos72 ~]# sed  -n   '6,/^u/p'   /etc/passwd
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
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

/pat/,#

[root@centos72 ~]# cat    -n   /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# sed -n /^f/,+5p /etc/passwd
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
[root@centos72 ~]# sed -n /^f/,15p /etc/passwd
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

(4) ~:步进
1~2 奇数行,表示从第1行开始打印,每2行打印一次
2~2 偶数行,表示从第2行开始打印,每2行打印一次

1)打印文件的奇数行

[root@centos72 ~]# sed  -n   1~2p   /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
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat /etc/passwd -n | sed -n 1~2p
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

2)打印文件的偶数行

[root@centos72 ~]#  cat  /etc/passwd   -n  |  sed    -n    2~2p
2 bin:x:1:1:bin:/bin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
8 halt:x:7:0:halt:/sbin:/sbin/halt
10 operator:x:11:0:operator:/root:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin

四编辑命令的使用

(一)d: 删除模式空间匹配的行,并立即启用下一轮循环

d: 删除模式空间匹配的行,并立即启用下一轮循环,也就是读入新的一行

(1)删除命令的第几行到第几行

删除文件的第6行到第16行

[root@centos72 ~]# cat  /etc/passwd   -n  |  sed    6,16d
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

(2)利用删除命令打印奇数行,也就是把偶数行删除了

法1:

[root@centos72 ~]#  cat  /etc/passwd   -n  |  sed    -n    1~2p
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

法2:

[root@centos72 ~]#  cat  /etc/passwd   -n  |  sed    -n    2~2d
[root@centos72 ~]# cat /etc/passwd -n | sed 2~2d
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

(3)把标准输入指定行删除

显示网络状态信息

[root@centos72 ~]# ss  -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*

删除网络状态信息的第1行

命令可以不加引号

[root@centos72 ~]# ss  -tnl   |  sed    1d
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@centos72 ~]# ss  -tnl   |  sed    '1d'
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*

打印以sd开头的内容

/是进行模式匹配的,和/dev里面的斜线冲突,所以要对非模式匹配的斜线进行转义

注意打印时最好都加上引号,否则会报错

[root@centos72 ~]# df  |   sed  -n     /^\/dev\/sd/p
sed: -e expression #1, char 5: extra characters after command
[root@centos72 ~]# df | sed -n '/^\/dev\/sd/p'
/dev/sda2 52403200 1119724 51283476 3% /
/dev/sda3 20961280 32944 20928336 1% /app
/dev/sda1 1038336 126596 911740 13% /boot

如果不转义就会报错

/pattern/:被此处模式所能够匹配到的每一行

[root@centos72 ~]# df  |   sed  -n     '/^/dev/sd/p'
sed: -e expression #1, char 5: extra characters after command

不可以使用其他符合进行分隔

[root@centos73 ~]#  df  |   sed  -n     '#^/dev/sd#p'
[root@centos73 ~]# df | sed -n '#^/dev/sd#p'

(二)a [\]text:在指定行后面追加文本,支持使用\n实现多行追加

(1)在文件的第5行到第10行的每行的下一行添加&&&&&&&&&

[root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a&&&&&&&&&'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 operator:x:11:0:operator:/root:/sbin/nologin
&&&&&&&&&
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

注意 一定要加引号

[root@centos65 ~]#  cat  -n  /etc/passwd   |  sed    5,10a&&&&&&&&&
-bash: syntax error near unexpected token `&&'

双引号也可以

[root@centos65 ~]#  cat  -n  /etc/passwd   |  sed    "5,10a&&&&&&&&&"
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
&&&&&&&&&
11 operator:x:11:0:operator:/root:/sbin/nologin
12 games:x:12:100:games:/usr/games:/sbin/nologin
13 gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
14 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
15 nobody:x:99:99:Nobody:/:/sbin/nologin
16 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
17 saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
20 dbus:x:81:81:System message bus:/:/sbin/nologin
21 ntp:x:38:38::/etc/ntp:/sbin/nologin
22 abrt:x:173:173::/etc/abrt:/sbin/nologin
23 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
24 tcpdump:x:72:72::/:/sbin/nologin
25 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
26 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
27 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
28 radvd:x:75:75:radvd user:/:/sbin/nologin
29 qemu:x:107:107:qemu user:/:/sbin/nologin
30 ident:x:98:98::/:/sbin/nologin

原文件的内容没有真正被修改

[root@centos72 ~]# cat  /etc/passwd  -n
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

(2)命令和文件之间有没有空格显示的结果都一样的。

[root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a      &&&&&&&&&'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 operator:x:11:0:operator:/root:/sbin/nologin
&&&&&&&&&
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat -n /etc/passwd | sed '5,10 a&&&&&&&&&'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 operator:x:11:0:operator:/root:/sbin/nologin
&&&&&&&&&
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

(3)如果要想打印出空格的,那么就要a后面加上斜线,斜线就相当于边界符

[\]表示此选项可有可无

[root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a\           &&&&&&&&&'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 operator:x:11:0:operator:/root:/sbin/nologin
&&&&&&&&&
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

注意斜线要和后面的内容有空格,至少是一个空格

[root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a         \&&&&&&&&&'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 operator:x:11:0:operator:/root:/sbin/nologin
&&&&&&&&&
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat  -n  /etc/passwd   |  sed    '5,10a           \                        &&&&&&&&&'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
&&&&&&&&&
6 sync:x:5:0:sync:/sbin:/bin/sync
&&&&&&&&&
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
&&&&&&&&&
8 halt:x:7:0:halt:/sbin:/sbin/halt
&&&&&&&&&
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
&&&&&&&&&
10 operator:x:11:0:operator:/root:/sbin/nologin
&&&&&&&&&
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 wang:x:1000:1000:wang:/home/wang:/bin/bash

实例:

在第8行之后追加

现在只是显示,没有真正追加到文件里面

因为模式匹配是单引号,所以里面的追加内容只能使用双引号,否则会引起冲突的

[root@centos72 ~]# sed  '8a   alias  cdne="cd   /etc/sysconfig/network-scripts/"'    ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

原文件内容保持不变

第8行是空行

[root@centos72 ~]# cat  -n   ~/.bashrc
1 # .bashrc
2
3 # User specific aliases and functions
4
5 alias rm='rm -i'
6 alias cp='cp -i'
7 alias mv='mv -i'
8
9 # Source global definitions
10 if [ -f /etc/bashrc ]; then
11 . /etc/bashrc
12 fi

(三)-i.bak: 备份文件并原处编辑

真正追加到文件里面,要谨慎使用,确定没有问题再确定追加,凡事三思而后行。

-i.bak: 备份文件并原处编辑,也就是先对原来的文件进行备份再修改,这样会更好。

我们在对某个服务的配置文件进行修改时也要养成这种好习惯,先备份再修改,运维工作最重要的是备份。

如果是追加到文件的最后一行,使用echo重定向就可以了

[root@centos72 ~]# sed   -i.bak    '8a   alias  cdne="cd   /etc/sysconfig/network-scripts/"'    ~/.bashrc
[root@centos72 ~]# cat -n ~/.bashrc
1 # .bashrc
2
3 # User specific aliases and functions
4
5 alias rm='rm -i'
6 alias cp='cp -i'
7 alias mv='mv -i'
8
9 alias cdne="cd /etc/sysconfig/network-scripts/"
10 # Source global definitions
11 if [ -f /etc/bashrc ]; then
12 . /etc/bashrc
13 fi
[root@centos72 ~]# ls -a ~/
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .bashrc.bak .cshrc .tcshrc .viminfo
#生成了备份文件.bashrc.bak

(四)i [\]text:在匹配的行前面插入文本

和a命令作用相似,结果相反。

使用到正则表达式,^表示行首

/^# Source/模式匹配,表示以# Source开头的行

[root@centos72 ~]# sed   -i.bak    '/^# Source/i$$$$$$$'    ~/.bashrc
[root@centos72 ~]# cat -n ~/.bashrc
1 # .bashrc
2
3 # User specific aliases and functions
4
5 alias rm='rm -i'
6 alias cp='cp -i'
7 alias mv='mv -i'
8
9 alias cdne="cd /etc/sysconfig/network-scripts/"
10 $$$$$$$
11 # Source global definitions
12 if [ -f /etc/bashrc ]; then
13 . /etc/bashrc
14 fi

和a一样,如果\和后面的文本没有空格那么和没加\是一样的结果

[root@centos72 ~]# sed      '/^# Source/i\**********'    ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
**********
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# sed '/^# Source/i\ **********' ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
**********
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos65 ~]# sed      '/^# Source/i\     **********'    ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' **********
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

对比:

a是在行后追加

[root@centos72 ~]# sed      '/^# Source/a**********'    ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
**********
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# sed '/^# Source/a\**********' ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
**********
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# sed '/^# Source/a \ **********' ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
**********
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

实现多行追加

这样就可以一次性加好几条别名

[root@centos72 ~]# cat   ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# sed '/^# Source/ixxxxxxxxxxxx\nyyyyyyyyyy\nzzzzzzzzzzzzz' ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
xxxxxxxxxxxx
yyyyyyyyyy
zzzzzzzzzzzzz
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

注意此时的家目录是root

所以~/.bashrc和/root/.bashrc是等价的

[root@centos72 ~]# cat  ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ka='systemctl restart keepalived.service' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# pwd
/root
[root@centos72 ~]# cat /root/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ka='systemctl restart keepalived.service' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos65 ~]# diff  /root/.bashr^C ~/.bashrc
[root@centos65 ~]# who
root pts/0 2019-01-25 05:32 (192.168.137.1)
[root@centos65 ~]# whoami
root

(五)c [\]text:替换行为单行或多行文本

[root@centos72 ~]# cat  -n   ~/.bashrc
1 # .bashrc
2
3 # User specific aliases and functions
4
5 alias rm='rm -i'
6 alias cp='cp -i'
7 alias mv='mv -i'
8
9 alias cdne="cd /etc/sysconfig/network-scripts/"
10 $$$$$$$
11 # Source global definitions
12 if [ -f /etc/bashrc ]; then
13 . /etc/bashrc
14 fi
[root@centos72 ~]# sed '/^# Source/c \ **********' ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
**********
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# sed      '/^# Source/c    **********'    ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
**********
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos65 ~]# cat  ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos65 ~]# sed      '/^# Source/c    **********'    ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' **********
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos65 ~]# cat ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

实例:

(1)实现一行替换

替换的很实用,特别是在修改配置文件的时候

/pattern/:被此处模式所能够匹配到的每一行

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@centos72 ~]# sed -i.bak '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@centos72 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

/^SELINUX=/表示匹配以SELINUX=开头的行

[root@centos65 ~]# cat  /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@centos65 ~]# #sed '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@centos65 ~]# sed '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

(六)w    /path/somefile: 保存模式匹配的行至指定文件

w    /path/somefile: 保存模式匹配的行至指定文件,要加上单引号才可以

[root@centos72 ~]# sed  /^alias/w    /app/alias.txt
sed: couldn't open file : No such file or directory
[root@centos72 ~]# sed /^alias/w /app/alias.txt ~/.bashrc
sed: couldn't open file : No such file or directory
[root@centos72 ~]# sed '/^alias/w /app/alias.txt' ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

执行结果,下面是自动创建的文件

[root@centos72 ~]# cat  /app/alias.txt
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ka='systemctl restart keepalived.service'

也就是把最后的文件匹配到的内容复制到中间指定的文件

[root@centos73 ~]# sed  '/^alias/w    /app/alias.txt'     ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos73 ~]# cat /app/alias.txt'
> ^C
[root@centos73 ~]# cat /app/alias.txt
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
[root@centos73 ~]# ll /app/alias.txt
-rw-r--r--. 1 root root 51 Jun 20 14:07 /app/alias.txt

使用双引号也可以

和变量一样要加引号

[root@centos72 ~]# sed  "/^alias/w    /app/as.txt"     ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ka='systemctl restart keepalived.service' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# cat /app/a
alias.txt as.txt
[root@centos72 ~]# cat /app/as.txt
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ka='systemctl restart keepalived.service'

(七)r  /path/somefile:读取指定文件的文本至模式空间中匹配到的行后

在alias开头的行后面追加文件的内容

[root@centos72 ~]# sed     '/^alias/r  /etc/centos-release'     ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
CentOS Linux release 7.5.1804 (Core)
alias cp='cp -i'
CentOS Linux release 7.5.1804 (Core)
alias mv='mv -i'
CentOS Linux release 7.5.1804 (Core) alias cdne="cd /etc/sysconfig/network-scripts/"
CentOS Linux release 7.5.1804 (Core)
$$$$$$$
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

=: 为模式空间中的行打印行号

[root@centos72 ~]# sed     '/^alias/='     ~/.bashrc
# .bashrc # User specific aliases and functions 5
alias rm='rm -i'
6
alias cp='cp -i'
7
alias mv='mv -i' 9
alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@centos72 ~]# cat -n .bashrc
1 # .bashrc
2
3 # User specific aliases and functions
4
5 alias rm='rm -i'
6 alias cp='cp -i'
7 alias mv='mv -i'
8
9 alias cdne="cd /etc/sysconfig/network-scripts/"
10 $$$$$$$
11 # Source global definitions
12 if [ -f /etc/bashrc ]; then
13 . /etc/bashrc
14 fi

(八)!:模式空间中匹配行取反处理

在条件测试里面!也表示取反

[root@centos72 ~]# sed     '/^alias/p'     ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias rm='rm -i'
alias cp='cp -i'
alias cp='cp -i'
alias mv='mv -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

注意!只能加在命令的前面

[root@centos72 ~]# sed     '/^alias/p!'     ~/.bashrc
sed: -e expression #1, char 10: extra characters after command
[root@centos72 ~]# sed '/^alias/!p' ~/.bashrc
# .bashrc
# .bashrc # User specific aliases and functions
# User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' alias cdne="cd /etc/sysconfig/network-scripts/"
$$$$$$$
$$$$$$$
# Source global definitions
# Source global definitions
if [ -f /etc/bashrc ]; then
if [ -f /etc/bashrc ]; then
. /etc/bashrc
. /etc/bashrc
fi
fi

取反之后就不打印alias开头的行了

[root@centos72 ~]# sed     -n    '/^alias/p'     ~/.bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cdne="cd /etc/sysconfig/network-scripts/"
[root@centos72 ~]# sed -n '/^alias/!p' ~/.bashrc
# .bashrc # User specific aliases and functions $$$$$$$
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

显示文件中以bash结尾的行,涉及到正则表达式$

[root@centos72 ~]# sed  -n    '/bash$/p'  /etc/passwd
root:x:0:0:root:/root:/bin/bash
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat /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
wang:x:1000:1000:wang:/home/wang:/bin/bash

打印除bash结尾的行

[root@centos72 ~]# sed  -n    '/bash$/!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
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

文本处理工具——sed基础的更多相关文章

  1. Shell 编程 文本处理工具 sed

    本篇主要写一些shell脚本文本处理工具sed的使用. 概述 sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除.替换.添加 ...

  2. 文本处理工具sed

    处理文本的工具sed  行编辑器 ,默认自带循环. sed是一种流编辑器,它一次处理一行内容. 功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等 sed工具 用法: sed ...

  3. Linux文本处理工具——Sed

    sed:数据流编辑器: awk:报告文本的生成器 sed 基本用法:(Stream EDitor) Stream 流 EDitor 编辑器 行编辑器 全屏编辑器:vi/vimsed:内存空间(模式空间 ...

  4. 文本处理工具——sed进阶

    一sed的搜索替代 (一)常见的和替代相关的选项 搜索替代,和vim的写法很像 s///:查找替换,支持使用其它分隔符,s@@@,s### p: 显示替换成功的行,就是打印. w /PATH/TO/S ...

  5. 轻松学会文本处理工具之二 linux sed命令

    sed命令的语法格式: sed的命令格式: sed [option]  'sed command'filename sed的脚本格式:sed [option] -f  'sed  script'fil ...

  6. Linux的文本处理工具浅谈-awk sed grep

    Linux的文本处理工具浅谈 awk   老大 [功能说明] 用于文本处理的语言(取行,过滤),支持正则 NR代表行数,$n取某一列,$NF最后一列 NR==20,NR==30 从20行到30行 FS ...

  7. Pyp 替代sed,awk的文本处理工具

    Linux上文本处理工具虽不少,像cut,tr,join,split,paste,sort,uniq,sed,awk这些经典工具让人眼花缭乱,而且都太老了,使用方法都不太人性化,尤其awk,语法简直反 ...

  8. 三大文本处理工具grep、sed及awk的简单介绍

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...

  9. 【Linux】 字符串和文本处理工具 grep & sed & awk

    Linux字符串&文本处理工具 因为用linux的时候主要用到的还是字符交互界面,所以对字符串的处理变得十分重要.这篇介绍三个常用的字符串处理工具,包括grep,sed和awk ■ grep ...

随机推荐

  1. 如何在某个apps包下面中创建APP

  2. MySQL/RDS数据如何同步到MaxCompute之实践讲解

    摘要:大数据计算服务(MaxCompute,原名ODPS)是阿里云提供的一种快速.完全托管的EB级数据仓库解决方案.本文章中阿里云MaxCompute公有云技术支持人员刘力夺通过一个实验向大家介绍了阿 ...

  3. git分支merger

  4. ajax 封装(集中 认证、错误、请求loading处理)

    一.为什么要对 ajax 进行封装:    (在使用antd pro 开发项目时,里面默认是把请求进行了封装的,放在 utils/request.js 中.使用起来非常方便   https://pro ...

  5. 左手Mongodb右手Redis redis操作

    set key value  设置key的值 get key 取得key的值 decr key 值会减一 incr key 值会加一 decrby key value ,会让key的值减少value. ...

  6. tp5怎么验证手机号码

    直接上干货

  7. 题解 P1017 【进制转换】

    我赶jio这个题难道是让我们写快写? 不管了,赶紧把咕咕咕了一万年的题解写出来. 这个题就是考察负进制和在mod意义下的除法运算的基础运算. (其实也没多大问题) 首先我们先假设一个原始数据\(num ...

  8. xcodebuild 自动化打包

    altool 文档 使用xcode自带的xcodebuild 命令通过脚本进行打包 打包->导出ipa, 两行关键的脚本代码 1.Archive xcodebuild archive -arch ...

  9. 嵌入式C语言4.1 C语言内存空间的使用-指针

    指针:就是内存资源的地址.门牌号的代名词 假如你所在的城市是一个内存(存储器),如果找到你家,就是通过你的家庭住址(指针)寻找,而你家里的摆设面积之类的就是内存的内容(指针指向的内容). 指针变量:存 ...

  10. QT pro文件的一种通用配置

    #设置UI文件目录 UI_DIR = ./ui CONFIG(debug, debug|release) {   #设置debug配置下编译生成文件的路径 TARGET = $$join(TARGET ...