标签:sed
sed每次处理一行,把读取的每一行读到模式空间(patternspace),再来判断此行是不是符合所给定的过虑条件,如果匹配,则执行相应的编辑操作(查找、删除、替换),再把编辑之后的内容输出到标准输出,如果不能被模式空间中的模式所匹配,则直接输出。不能被模式空间所匹配则默认是直接输出的。-n参数就是阻止这种行为的。编辑完了之后就放到了保持空间中。
sed [OPTION]... ‘script‘ [input-file] ...
script:地址定界编辑命令
-n:不输出模式空间中的内容至屏幕;
-e:多点编辑;
-f /filename 每行一个编辑命令;
-r:支持使用扩展正则表达式(默认只支持基本正则表达式);
-i:直接编辑原文件 ;
空地址:对全文进行处理,什么也不加
单地址:
n:指定是第n行;
/pattern/:被此模式所匹配到的每一行;
地址范围
n,m #表示从第n行到第m行
n,+m #表示从第n行到第n+m行,包含第n行
n,/pat1/ #从第n行匹配到模式1之间的行
/pat1/,/pat2/ #从模式1匹配到模式2之间的行
$:最后一行;
步进:~
1~2:所有奇数行
2~2:所有偶数行
d:删除;
p:显示模式空间中的内容;
a \text:在行后面追加文本“text”,支持使用\n实现多行追加; 注意:在当前行的下一行插入
i \text:在行前面插入文本“text”,支持使用\n实现多行插入; 注意:在当前行的上一行插入
c \text:把匹配到的行替换为此处指定的文本“text”;注意:这是整行替换
w /filename:保存模式空间匹配到的行至指定的文件中;
r /filename:读取指定文件的内容至当前文件被模式匹配到的行后面;文件合并;
y:转换命令,可以实现对单个字符进行转换
l(小写)列出行,允许打印数据流中的文本和不可打印的ASCII字符,比如\t来代表制表符
=:为模式匹配到的行打印行号,
!:条件取反;格式:地址定界!编辑命令; 注意:!一定要编辑命令的前面
s///:查找替换,其分隔符可自行指定,常用的有s@@@, s###等;
替换标记:
i. numbers:表明新文本将替换第几处模式匹配的地方
ii. g:全局替换;
iii. w /filename:将替换成功的结果保存至指定文件中;
iv. p:显示替换成功的行;
[root@Linux_11 ~]# sed ‘/^UUID/d‘fstab #把开头是UUID的行都删除
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# sed -n ‘/^UUID/d‘fstab #这个地方加上-n之后什么也不输出
[root@Linux_11 ~]#
注意:
为什么加上-n参数之后什么也不输出了?因为默认是将当前行放到模式空间中,使用模式进行匹配,默认行为是如果当前行没有被模式空间所匹配,则输出当前行。-n参数也是阻止这种默认行为的。
[root@Linux_11 ~]# sed /^$/d fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# sed ‘1~2p‘ a.txt # 奇数行显示了两遍
1
1
2 #
3 # /etc/fstab
3 # /etc/fstab
4 # Created by anacondaon Sat Nov 14 23:49:36 2015
5 #
5 #
6 # Accessiblefilesystems, by reference, are maintained under ‘/dev/disk‘
7 # See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 # See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242/ ext3 defaults 1 1
9 UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242/ ext3 defaults 1 1
10 UUID=8456c142-425f-4ee7-b196-388c62a4fc4f/boot ext4 defaults 1 2
11 UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d/home ext2 defaults 1 2
11 UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d/home ext2 defaults 1 2
12 UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881swap swap defaults 0 0
13 tmpfs /dev/shm tmpfs defaults 0 0
注意:
为什么奇数行显示了两遍?因为当前行被模式空间的模式所匹配了,-p是打印被匹配的内容,需要输出,这是第一次输出奇数行,第二次输出是因为当前行如果没有被模式空间中的模式所匹配也会默认输出。因为-p不属于编辑命令。
[root@Linux_11 ~]# sed -n ‘1~2p‘ a.txt
1
3 # /etc/fstab
5 #
7 # See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
9 UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242/ ext3 defaults 1 1
11 UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d/home ext2 defaults 1 2
13 tmpfs /dev/shm tmpfs defaults 0 0
15 sysfs /sys sysfs defaults 0 0
[root@Linux_11 ~]# sed ‘3a new time‘a.txt
1
2 #
3 # /etc/fstab
new time
4 # Created by anacondaon Sat Nov 14 23:49:36 2015
5 #
6 # Accessiblefilesystems, by reference, are maintained under ‘/dev/disk‘
7 # See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242/ ext3 defaults 1 1
10 UUID=8456c142-425f-4ee7-b196-388c62a4fc4f/boot ext4 defaults 1 2
11 UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d/home ext2 defaults 1 2
12 UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881swap swap defaults 0 0
13 tmpfs /dev/shm tmpfs defaults 0 0
14 devpts /dev/pts devpts gid=5,mode=620 0 0
15 sysfs /sys sysfs defaults 0 0
16 proc /proc proc defaults 0 0
[root@Linux_11 ~]# sed ‘3a \new time‘a.txt
1
2 #
3 # /etc/fstab
new time
4 # Created by anacondaon Sat Nov 14 23:49:36 2015
5 #
6 # Accessiblefilesystems, by reference, are maintained under ‘/dev/disk‘
7 # See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242/ ext3 defaults 1 1
10 UUID=8456c142-425f-4ee7-b196-388c62a4fc4f/boot ext4 defaults 1 2
11 UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d/home ext2 defaults 1 2
12 UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881swap swap defaults 0 0
13 tmpfs /dev/shm tmpfs defaults 0 0
14 devpts /dev/pts devpts gid=5,mode=620 0 0
15 sysfs /sys sysfs defaults 0 0
16 proc /proc proc defaults 0 0
[root@Linux_11 ~]# sed ‘3a \new time\nanother new line‘ a.txt #此another前面需要加\n
1
2 #
3 # /etc/fstab
new time
another new line
4 # Created by anacondaon Sat Nov 14 23:49:36 2015
5 #
6 # Accessiblefilesystems, 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=e5485f66-1a07-4de3-aadf-fe3d65fc5242/ ext3 defaults 1 1
10 UUID=8456c142-425f-4ee7-b196-388c62a4fc4f/boot ext4 defaults 1 2
11 UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d/home ext2 defaults 1 2
12 UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881swap swap defaults 0 0
13 tmpfs /dev/shm tmpfs defaults 0 0
14 devpts /dev/pts devpts gid=5,mode=620 0 0
15 sysfs /sys sysfs defaults 0 0
16 proc /proc proc defaults 0 0
[root@Linux_11 ~]# cat issue
REHL release 6.5 (Final)
Kernel \r on an \m
[root@Linux_11 ~]# sed ‘2i system‘ issue
REHL release 6.5 (Final)
system
Kernel \r on an \m
#############################################################
提示:如果在i的前面不指定行的话,则在每一行都插入后面的内容
[root@Linux_11 ~]# sed ‘i system‘ issue
system
REHL release 6.5 (Final)
system
Kernel \r on an \m
system
#############################################################
[root@Linux_11 ~]# sed ‘$i system‘ issue
REHL release 6.5 (Final)
Kernel \r on an \m
system
提示:要在文件的最后一行插入内容,需要使用“$”
#############################################################
在某一行的后面插入多行
[root@Linux_11 ~]# cat line
1
2
3
4
5
6
[root@Linux_11 ~]# sed ‘3i\
mysql\
php\
linux‘ line #提示:这个地方必须有一个空格
1
2
mysql
php
linux
3
4
5
6
[root@Linux_11 ~]# sed ‘/^UUID/c #new line‘ fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
#new line
#new line
#new line
#new line
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# sed ‘/^[^#]/w/tmp/fstab‘ fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# cat /tmp/fstab
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
[root@Linux_11 ~]# sed ‘3r /etc/issue‘fstab
#
# /etc/fstab
CentOS release 6.5 (Final)
Kernel \r on an \m
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# cat file2
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@Linux_11 ~]# sed ‘y/12/ab/‘ file2
This is line number a.
This is line number b.
This is line number 3.
This is line number 4.
注意:
1. 替换字符的个数必须一一对应
2. 只能是单个字符之间的映射替换
########################################################
[root@Linux_11 ~]# sed ‘1y/12/ab/‘ file2
This is line number a.
This is line number 2.
This is line number 3.
This is line number 4.
[root@Linux_11 ~]# sed ‘2y/12/ab/‘ file2
This is line number 1.
This is line number b.
This is line number 3.
This is line number 4.
提示:可以加上行号指定要替换的行中的指定的某些字符
[root@Linux_11 ~]# cat passwd
root:x:0:0:超级管理员:/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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[root@Linux_11 ~]# sed ‘s/bin/abc/‘passwd
root:x:0:0:超级管理员:/root:/abc/bash
abc:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sabc:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sabc/nologin
lp:x:4:7:lp:/var/spool/lpd:/sabc/nologin
sync:x:5:0:sync:/sabc:/bin/sync
shutdown:x:6:0:shutdown:/sabc:/sbin/shutdown
halt:x:7:0:halt:/sabc:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sabc/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sabc/nologin
operator:x:11:0:operator:/root:/sabc/nologin
games:x:12:100:games:/usr/games:/sabc/nologin
gopher:x:13:30:gopher:/var/gopher:/sabc/nologin
提示:不加任何替换标记只是替换被匹配到的行的第一个被匹配到的内容
[root@Linux_11 ~]# cat file
the quick green fox jumps over the lazy cat.
the quick green fox jumps over the lazy cat.
the quick green fox jumps over the lazy cat.
the quick green fox jumps over the lazy cat.
[root@Linux_11 ~]# sed ‘s/the/THE/g‘file
THE quick green fox jumps over THE lazy cat.
THE quick green fox jumps over THE lazy cat.
THE quick green fox jumps over THE lazy cat.
THE quick green fox jumps over THE lazy cat.
[root@Linux_11 ~]# sed ‘s/the/THE/‘ file
THE quick green fox jumps over the lazy cat.
THE quick green fox jumps over the lazy cat.
THE quick green fox jumps over the lazy cat.
THE quick green fox jumps over the lazy cat.
[root@Linux_11 ~]# cat file
the quick green the fox jumps over the lazy cat.
the quick green the fox jumps over the lazy cat.
the quick green the fox jumps over the lazy cat.
the quick green the fox jumps over the lazy cat.
[root@Linux_11 ~]# sed ‘s/the/THE/‘ file
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
[root@Linux_11 ~]# sed ‘s/the/THE/p‘ file
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
[root@Linux_11 ~]# sed -n ‘s/the/THE/p‘file
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
THE quick green the fox jumps over the lazy cat.
[root@Linux_11 ~]# cat passwd
root:x:0:0:超级管理员:/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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@Linux_11 ~]# sed ‘3s/sbin/SBIN/‘passwd
root:x:0:0:超级管理员:/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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
提示:此时不使用g替换标记只替换第3行的第一个sbin
此时若加上g替换标记则:
[root@Linux_11 ~]# sed ‘3s/sbin/SBIN/g‘passwd
root:x:0:0:超级管理员:/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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@Linux_11 ~]# cat issue
REHL release 6.5 (Final)
Kernel \r on an \m
[root@Linux_11 ~]# sed‘s/Kernel/CentOS/w new_issue‘ issue
REHL release 6.5 (Final)
CentOS \r on an \m
[root@Linux_11 ~]# cat new_issue
CentOS \r on an \m
[root@Linux_11 ~]# sed ‘/^UUID/r /etc/issue‘fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
CentOS release 6.5 (Final)
Kernel \r on an \m
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
CentOS release 6.5 (Final)
Kernel \r on an \m
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
CentOS release 6.5 (Final)
Kernel \r on an \m
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
CentOS release 6.5 (Final)
Kernel \r on an \m
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
等号命令会打印行在数据流中的当前行号。行号由数据流中的换行符决定。
[root@Linux_11 ~]# cat fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@Linux_11 ~]# sed = fstab
1
2
#
3
# /etc/fstab
4
# Created by anaconda on Sat Nov 14 23:49:36 2015
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) formore info
8
#
9
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
10
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
11
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
12
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
13
tmpfs /dev/shm tmpfs defaults 0 0
14
devpts /dev/pts devpts gid=5,mode=620 0 0
15
sysfs /sys sysfs defaults 0 0
16
proc /proc proc defaults 0 0
17
18
[root@Linux_11 ~]# sed -n ‘l ‘ fstab
$
#$
# /etc/fstab$
# Created by anaconda on Sat Nov 14 23:49:36 2015$
#$
# Accessible filesystems, by reference, are maintained under‘/dev/di\
sk‘$
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formor\
e info$
#$
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext\
3 defaults 1 1$
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext\
4 defaults 1 2$
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext\
2 defaults 1 2$
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swa\
p defaults 0 0$
tmpfs /dev/shm tmpfs defaults \
0 0$
devpts /dev/pts devpts gid=5,mode=62\
0 0 0$
sysfs /sys sysfs defaults \
0 0$
proc /proc proc defaults \
0 0$
$
/dev/cdrom /mnt iso9660 defaults 0 0 $
[root@Linux_11 ~]# cat fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# sed -e‘s/^#[[:space:]]*//‘ -e ‘/^UUID/d‘ fstab
/etc/fstab
Created by anaconda on Sat Nov 14 23:49:36 2015
Accessible filesystems, by reference, are maintained under‘/dev/disk‘
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for moreinfo
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@Linux_11 ~]# cat fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@Linux_11 ~]# sed -e ‘/^#/d; /^UUID/d‘ fstab
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
提示:可以使用一个“-e”参数,然后使用引号将多个命令括起来,一定要使用分号“;”进行分隔。
[root@Linux_11 ~]# cat file
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
[root@Linux_11 ~]# sed -e ‘
> s/green/red/
> s/cat/tac/
> s/The/That/‘ file
That quick red fox jumps over the lazy tac.
That quick red fox jumps over the lazy tac.
That quick red fox jumps over the lazy tac.
That quick red fox jumps over the lazy tac.
提示:注意写的格式,file不能另写一行。
[root@Linux_11 ~]# cat issue
CentOS release 6.5(Final)
Kernel \r on an \m
[root@Linux_11 ~]# sed -i‘s/CentOS/REHL/‘ issue
[root@Linux_11 ~]# cat issue
REHL release 6.5(Final)
Kernel \r on an \m
[root@Linux_11 ~]# cat scr
s/^UUID[[:space:]]*/This/
/^#/d
[root@Linux_11 ~]# cat fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@Linux_11 ~]# sed -f scr fstab
This=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
This=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
This=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
This=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@Linux_11 ~]# cat scr
s/^UUID[[:space:]]*/This/; /^#/d
提示:此种写法必须使用分号隔开。
[root@Linux_11 ~]# cat fstab
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
UUID=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
UUID=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
UUID=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
UUID=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@Linux_11 ~]# sed -f scr fstab
This=e5485f66-1a07-4de3-aadf-fe3d65fc5242 / ext3 defaults 1 1
This=8456c142-425f-4ee7-b196-388c62a4fc4f /boot ext4 defaults 1 2
This=0b3dbe42-03c3-408a-b634-1b6c60d2640d /home ext2 defaults 1 2
This=00d9e852-7ba0-4a6c-b2a8-34a73e2e4881 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
[root@Linux_11 ~]#sed ‘/^#/!d‘ fstab #注意!需要在d命令的前面
#
# /etc/fstab
# Created by anaconda on Sat Nov 14 23:49:36 2015
#
# Accessible filesystems, by reference, are maintained under‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) formore info
#
本文出自 “静心&清心” 博客,谢绝转载!
标签:sed
原文地址:http://jxqx1991.blog.51cto.com/10927800/1728717