★示例
1.d: 删除模式空间匹配的行
[root@localhost ~]# cat -n /etc/fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Thu Nov 30 17:46:50 2017
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=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
10 UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
11 UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
12 /dev/sr0 /media/cdrom iso9660 defaults 0 0
[root@localhost ~]# cat -n /etc/fstab > sed.txt
# 删除1到5行的内容
[root@localhost ~]# sed ‘1,5d’ sed.txt
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=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
10 UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
11 UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
12 /dev/sr0 /media/cdrom iso9660 defaults 0 0
# 删除以UUID开头的行,被模式 /^UUID/ 匹配到的每一行
[root@localhost ~]# sed ‘/^UUID/d’ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Nov 30 17:46:50 2017
#
# 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/sr0 /media/cdrom iso9660 defaults 0 0
# 删除以#开头的行
[root@localhost ~]# sed ‘/^#/d’ /etc/fstab
UUID=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
/dev/sr0 /media/cdrom iso9660 defaults 0 0
# 删除奇数行,显示偶数行
[root@localhost ~]# sed ‘1~2d’ sed.txt
2 #
4 # Created by anaconda on Thu Nov 30 17:46:50 2017
6 # Accessible filesystems, by reference, are maintained under ‘/dev/disk’
8 #
10 UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
12 /dev/sr0 /media/cdrom iso9660 defaults 0 0
2.p: 打印模式空间中的内容
[root@localhost ~]# sed ‘3d’ sed.txt
1
2 #
4 # Created by anaconda on Thu Nov 30 17:46:50 2017
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=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
10 UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
11 UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
12 /dev/sr0 /media/cdrom iso9660 defaults 0 0
# -n 不显示模式空间中的内容,
[root@localhost ~]# sed -n ‘3d’ sed.txt
# 首先显示模式空间中没有被匹配到的行(偶数行),然后再显示被匹配到的指定的行,执行p命令打印出第3行,
# 因为默认模式空间中的内容都会被显示出来,所以第三行还会再显示一遍。对比d命令,是把模式空间中匹配到的行
# 删除,就显示不出来了
[root@localhost ~]# sed ‘3p’ sed.txt
1
2 #
3 # /etc/fstab
3 # /etc/fstab
4 # Created by anaconda on Thu Nov 30 17:46:50 2017
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=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
10 UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
11 UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
12 /dev/sr0 /media/cdrom iso9660 defaults 0 0
# 首先,-n选项指明不显示模式空间中的内容,然后命令p指明显示指定的行
[root@localhost ~]# sed -n ‘3p’ sed.txt
3 # /etc/fstab
[root@localhost ~]# sed -n ‘1~2p’ sed.txt
1
3 # /etc/fstab
5 #
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
9 UUID=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
11 UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
3.i \text,a \text和c \text:在行后面追加文本;支持使用\n实现多行追加
# 默认会显示模式空间中的所有内容(因为没有对第3行执行d命令,所以也会显示出来),然后再在行前插入一行
[root@localhost ~]# sed ‘3i \new lines’ sed.txt
1
2 #
new lines
3 # /etc/fstab
4 # Created by anaconda on Sun Nov 6 10:30:14 2016
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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
10 UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
11 UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
12 UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
# 在行后插入一行
[root@localhost ~]# sed ‘3a \new lines’ sed.txt
1
2 #
3 # /etc/fstab
new lines
4 # Created by anaconda on Sun Nov 6 10:30:14 2016
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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
10 UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
11 UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
12 UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
# \n 实现多行追加
[root@localhost ~]# sed ‘3a \new lines\ntaotao\nxiuxiu’ sed.txt
1
2 #
3 # /etc/fstab
new lines
taotao
xiuxiu
4 # Created by anaconda on Sun Nov 6 10:30:14 2016
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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
10 UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
11 UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
12 UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
#在匹配的模式后增加内容
[root@localhost ~]# sed ‘/^UUID/a \# add new line base on UUID’ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 6 10:30:14 2016
#
# 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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
# add new line base on UUID
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
# add new line base on UUID
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
# add new line base on UUID
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
# add new line base on UUID
# 将指定的行替换为此处的“text”
[root@localhost ~]# sed ‘/^UUID/c \# add new line base on UUID’ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 6 10:30:14 2016
#
# 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
#
# add new line base on UUID
# add new line base on UUID
# add new line base on UUID
# add new line base on UUID
4.w /path/to/somefile:把指定的内容另存至/path/to/somefile路径所指定的文件中。
[root@localhost ~]# sed -n ‘/^[^#]/p’ /etc/fstab
UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
[root@localhost ~]# sed ‘/^[^#]/w /tmp/fstab.new’ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 6 10:30:14 2016
#
# 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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
[root@localhost ~]# cat /tmp/fstab.new
UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
5.r /path/from/somefile:在文件的指定位置插入另一个文件的所有内容,完成文件合并。
[root@localhost ~]# sed ‘3r /etc/issue’ sed.txt
1
2 #
3 # /etc/fstab
\S
Kernel \r on an \m
Mage Education Learning Services
http://www.magedu.com
4 # Created by anaconda on Sun Nov 6 10:30:14 2016
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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
10 UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
11 UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
12 UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
6.= : 为模式空间中的行打印行号
[root@localhost ~]# sed ‘/^UUID/=’ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 6 10:30:14 2016
#
# 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
#
9
UUID=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
10
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
11
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
12
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
7.! :模式空间中匹配行取反处理
# 删除非#号开头的行,注意“!”的位置
[root@localhost ~]# sed ‘/^#/!d’ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 6 10:30:14 2016
#
# 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
#
8.s///查找替换,及多点编辑
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 6 10:30:14 2016
#
# 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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
# 删除/etc/fstab文件中所有以#开头的行的行首的#及#后面所有的空白字符
[root@localhost ~]# sed ‘s@^#[[:space:]]*@@’ /etc/fstab
/etc/fstab
Created by anaconda on Sun Nov 6 10:30:14 2016
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=7bdf8e89-59c8-425e-a108-5c7c115e0afe / xfs defaults 0 0
UUID=3360e49a-d492-4f98-9957-edcb4db93384 /boot xfs defaults 0 0
UUID=eb3cdf15-b9e4-4ea6-b28b-75d4b4a54df8 /usr xfs defaults 0 0
UUID=18deb1ed-ee42-4269-94f3-6791304344e8 swap swap defaults 0 0
# 多点编辑,同时把UUID开头的行删除
[root@localhost ~]# sed -e ‘s@^#[[:space:]]*@@’ -e ‘/^UUID/d’ /etc/fstab
/etc/fstab
Created by anaconda on Sun Nov 6 10:30:14 2016
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
#删除/boot/grub/grub2.cfg文件中所有以空白字符开头的行的行首的所有空白字符
[root@localhost ~]# sed ‘s@^[[:space:]]\+@@’ /etc/grub2.cfg
#删除/etc/fstab文件中所有以#开头的行的行首的#号及#后面的所有空白字符
[root@localhost ~]# sed ‘s@^#[[:space:]]*@@’ /etc/fstab
#输出一个绝对路径给sed命令,取出其目录,其行为类似于dirname;
[root@localhost ~]# echo “/var/log/messages/” | sed ‘s@[^/]\+/\?$@@’
[root@localhost ~]# echo “/var/log/messages” | sed -r ‘s@[^/]+/?$@@’
评论列表(1条)
非常赞的博文~希望能继续保持~加油~