Shell中的sed编辑器

一.sed行编辑器的简介
全称:stream editor
作用:用来操作纯ASCII码的文本。在处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间“(pattern space)可以指定仅仅处理哪些行。
sed符合模式条件的处理,不符合条件的不予处理;处理完成之后把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复,直到文件的末尾。
二.sed命令的调用格式
1.sed [options] ‘command’ file(s) ##以命令的方式进行运行

[[email protected] mnt]# vim passwd
[[email protected] mnt]# cat 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
[[email protected] mnt]# sed 's/:/###/g' 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

2.sed [options] -f scriptfile file(s) ##以脚本的方式进行运行

[[email protected] mnt]# vim rule
[[email protected] mnt]# cat rule  ##将需要执行的命令写入脚本中
s/:/###/g
[[email protected] mnt]# sed -f rule 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

三.sed对字符的处理
1.p模式操作 ##显示
(1)sed -n “/#/p” fstab ##显示带#的行

[[email protected] mnt]# cp /etc/fstab .
[[email protected] mnt]# vim fstab
[[email protected] mnt]# cat fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
asdjkcjdjdsjs #
[[email protected] mnt]# sed -n "/#/p" fstab
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
asdjkcjdjdsjs #

(2)sed -n “/^#/p” fstab ##显示以#开头的行

[[email protected] mnt]# sed -n "/^#/p" fstab
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#

(3)sed -n “/#$/p” fstab ##显示以#结尾的行

[[email protected] mnt]# sed -n "/#$/p" fstab
#
#
#
asdjkcjdjdsjs #

(4)sed -n ‘/#$/!p’ fstab ##显示不是以#结尾的行

[[email protected] mnt]# sed -n '/#$/!p' fstab

# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0

(5)sed -n ‘/#/!p’ fstab ##显示不含#的行

[[email protected] mnt]# sed -n '/#/!p' fstab

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0

(6)sed -n ‘/^$/p’ fstab ##显示以空行开头和结尾的

[[email protected] mnt]# sed -n '/^$/p' fstab

(7)sed -n ‘/^$/!p’ fstab ##显示不是以空行开头和结尾的

[[email protected] mnt]# sed -n '/^$/!p' fstab
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
asdjkcjdjdsjs #

(8)cat -n fstab | sed -n ‘2,6p’ ##显示2-6行,cat -n 计算空行

[[email protected] mnt]# cat -b fstab

     1	#
     2	# /etc/fstab
     3	# Created by anaconda on Wed May  7 01:22:57 2014
     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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
     9	/dev/vg0/vo	/home	ext4	defaults	0 0
    10	asdjkcjdjdsjs #
[[email protected] mnt]# cat -n fstab
     1	
     2	#
     3	# /etc/fstab
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
    11	asdjkcjdjdsjs #
[[email protected] mnt]# cat -n fstab | sed -n '2,6p'
     2	#
     3	# /etc/fstab
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     5	#
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'

(9)cat -n fstab | sed -n ‘2,6!p’ ##显示除过2-6行以外的

[[email protected] mnt]# cat -n fstab | sed -n '2,6!p'
     1	
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
    11	asdjkcjdjdsjs #

(10)sed -n ‘/UUID/p’ fstab ##显示含UUID的行

[[email protected] mnt]# sed -n '/UUID/p' fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

2.d模式操作 ##删除
(1)sed ‘/UUID/d’ fstab ##删除含UUID的行

[[email protected] mnt]# sed -n '/UUID/d' fstab  ##-n删除但不显示结果
[[email protected] mnt]# sed  '/UUID/d' fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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/vg0/vo	/home	ext4	defaults	0 0
asdjkcjdjdsjs #

(2)sed ‘/^#/d’ fstab ##删除以#开头的行

[[email protected] mnt]# sed  '/^#/d' fstab

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
asdjkcjdjdsjs #

(3)sed -e ‘/#$/d’ -e ‘/^#/d’ fstab ##删除以#开头和结尾的

[[email protected] mnt]# sed -e '/#$/d' -e '/^#/d' fstab

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
[[email protected] mnt]# sed '/#$/d;/^#/d' fstab

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0

(4)sed ‘/^UUID/d’ fstab ##删除以UUID开头的

[[email protected] mnt]# sed '/^UUID/d' fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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/vg0/vo	/home	ext4	defaults	0 0
asdjkcjdjdsjs #

(5)sed ‘/^UUID/!d’ fstab ##删除不是以UUID开头的

[[email protected] mnt]# sed '/^UUID/!d' fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

(6)以脚本方式运行:将用户名单里的用户创建出来,存在则显示用户存在,不存在则把用户创建除来

[[email protected] mnt]# vim createuser.sh

Shell中的sed编辑器

[[email protected] mnt]# sh createuser.sh
Error:Please input a userfile folling scripts!!
[[email protected] mnt]# sh createuser.sh userfile
Error:userfile is not exists!!
[[email protected] mnt]# vim userfile
[[email protected] mnt]# cat userfile
westos1
westos2
westos3
[[email protected] mnt]# sh createuser.sh userfile
westos1 is exist!!
westos2 is exist!!
westos3 is exist!!

3.a模式操作 ##添加

[[email protected] mnt]# sed '/UUID/a hello westos' fstab  ##在UUID行添加hello westos,一般是在行尾下一行添加

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
hello westos
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#

4.c模式操作 ##替换

[[email protected] mnt]# sed '/UUID/c hello westos' fstab  ##将UUID行替换成hello westos

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
hello westos
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#
[[email protected] mnt]# sed '/UUID/cUUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1 hello westos' fstab  ##在UUID行行尾添加hello westos

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1 hello westos
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#

5.w模式操作 ##写入
(1)sed ‘/^UUID/w file’ fstab ##将以UUID开头的行写入file文件中

[[email protected] mnt]# sed '/^UUID/w file' fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#
[[email protected] mnt]# cat file
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

(2)cat -n fstab | sed ‘6r /mnt westos’ ##将westos文件中的字符写入fstab的第6行

[[email protected] mnt]# vim westos
[[email protected] mnt]# cat westos
hahahaha
[[email protected] mnt]# cat -n fstab | sed '6r /mnt/westos'
     1	
     2	#
     3	# /etc/fstab
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     5	#
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
hahahaha
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
    11	gdwuehfjkrejgtrkr#

6.i模式操作 ##插入

[[email protected] mnt]# sed '/UUID/i hello westos' fstab  ##在UUID行插入hello westos,一般是在行首上一行添加

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
hello westos
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#
[[email protected] mnt]# sed '/UUID/i hello\nwestos' fstab  ##换行插入hello westos  \n  ##表示换行

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
hello
westos
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#

四.sed的其他用法
1.sed -n ‘/^UUID/=’ fstab ##仅显示UUID所在的行

[[email protected] mnt]# sed -n '/^UUID/=' fstab
9

2.sed ‘/^UUID/=’ fstab ##显示UUID开头所在的行,和文件所有内容一起显示

[[email protected] mnt]# sed '/^UUID/=' fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo	/home	ext4	defaults	0 0
gdwuehfjkrejgtrkr#

3.sed -n ‘/UUID=;/UUID/p’ fstab ##显示UUID开头所在的行及行数

[[email protected] mnt]# sed -n '/^UUID/=;/^UUID/p' fstab
9
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

4.sed ‘s/sbin/westos/g’ passwd ##将文件中的所有sbin替换成westos

[[email protected] mnt]# vim passwd
[[email protected] mnt]# cat 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
[[email protected] mnt]# sed 's/sbin/westos/g' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/westos/nologin
daemon:x:2:2:daemon:/westos:/westos/nologin
adm:x:3:4:adm:/var/adm:/westos/nologin
lp:x:4:7:lp:/var/spool/lpd:/westos/nologin
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/nologin
operator:x:11:0:operator:/root:/westos/nologin

5.sed ‘1,5s/sbin/westos/g’ passwd ##将1-5行的sbin替换成westos

[[email protected] mnt]# sed '1,5s/sbin/westos/g' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/westos/nologin
daemon:x:2:2:daemon:/westos:/westos/nologin
adm:x:3:4:adm:/var/adm:/westos/nologin
lp:x:4:7:lp:/var/spool/lpd:/westos/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

6.sed ‘/lp/,/halt/s/sbin/westos/g’ passwd ##将lp及halt行的sbin替换成westos

[[email protected] mnt]# sed '/lp/,/halt/s/sbin/westos/g' 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:/westos/nologin
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

7.sed -f file passwd ##运行执行文件实现sed对某文件的操作

[[email protected] mnt]# vim file
[[email protected] mnt]# sed -f file 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:/westos/nologin
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] mnt]# cat file
/lp/,/halt/s/sbin/westos/g

8.sed ‘s/// /g’ passwd ##替换passwd文件中的/ \ ##转译,未转译时/相当于一关键字具有特殊含义;转译后/仅代表一个字符符号无特殊含义

[[email protected] mnt]# sed 's/\// /g' 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

9.sed ‘[email protected]/@ @g’ passwd ##替换passwd中的/

[[email protected] mnt]# sed '[email protected]/@ @g' 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

10.sed ‘G’ fstab ##在文件的每一行后面再加一行空行

[[email protected] mnt]# sed 'G' fstab


#

# /etc/fstab

# Created by anaconda on Wed May  7 01:22:57 2014

#

# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

/dev/vg0/vo	/home	ext4	defaults	0 0

11.sed ‘$!G’ fstab ##除过最后一行,其余每行后面再加一行空行

[[email protected] mnt]# sed '$!G' fstab


#

# /etc/fstab

# Created by anaconda on Wed May  7 01:22:57 2014

#

# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

/dev/vg0/vo	/home	ext4	defaults	0 0

12.sed ‘=’ fstab ##在每一行的前一行显示行号

[[email protected] mnt]# sed '=' fstab
1

2
#
3
# /etc/fstab
4
# Created by anaconda on Wed May  7 01:22:57 2014
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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
10
/dev/vg0/vo	/home	ext4	defaults	0 0

13.sed ‘=’ fstab | sed ‘N;G’ ##每一行的前一行显示行号的同时,在每一行后添加空行

[[email protected] mnt]# sed '=' fstab | sed 'N;G'
1


2
#

3
# /etc/fstab

4
# Created by anaconda on Wed May  7 01:22:57 2014

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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

10
/dev/vg0/vo	/home	ext4	defaults	0 0

14.sed ‘=’ fstab | sed ‘N;s/\n//g’ ##在每一行行首显示行号

[[email protected] mnt]# sed '=' fstab | sed 'N;s/\n//g'
1
2#
3# /etc/fstab
4# Created by anaconda on Wed May  7 01:22:57 2014
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#
9UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
10/dev/vg0/vo	/home	ext4	defaults	0 0

15.sed -n ‘1p’ fstab ##显示第一行

[[email protected] mnt]# sed -n '1p' fstab

五.测试
在Apache服务中实现80端口的自动更改

[[email protected] mnt]# vim apache_port.sh

Shell中的sed编辑器

[[email protected] mnt]# sh apache_port.sh 
ERROR:Apache is not running!!
[[email protected] mnt]# systemctl start httpd
[[email protected] mnt]# sh apache_port.sh 
ERROR:Please input port number folling scripts!!
[[email protected] mnt]# sh apache_port.sh 22
ERROR:22 port is in used!!
[[email protected] mnt]# sh apache_port.sh 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      0          79906      7068/httpd          
[[email protected] mnt]# sh apache_port.sh 8888
tcp6       0      0 :::8888                 :::*                    LISTEN      0          80659      7130/httpd