Shell中的sed编辑器

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

[root@dns-server mnt]# vim passwd
[root@dns-server 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
[root@dns-server 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) ##以脚本的方式进行运行

[root@dns-server mnt]# vim rule
[root@dns-server mnt]# cat rule  ##将需要执行的命令写入脚本中
s/:/###/g
[root@dns-server 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 ##显示带#的行

[root@dns-server mnt]# cp /etc/fstab .
[root@dns-server mnt]# vim fstab
[root@dns-server 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 #
[root@dns-server 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 ##显示以#开头的行

[root@dns-server 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 ##显示以#结尾的行

[root@dns-server mnt]# sed -n "/#$/p" fstab
#
#
#
asdjkcjdjdsjs #

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

[root@dns-server 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 ##显示不含#的行

[root@dns-server 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 ##显示以空行开头和结尾的

[root@dns-server mnt]# sed -n '/^$/p' fstab

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

[root@dns-server 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 计算空行

[root@dns-server 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 #
[root@dns-server 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 #
[root@dns-server 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行以外的

[root@dns-server 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的行

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

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

[root@dns-server mnt]# sed -n '/UUID/d' fstab  ##-n删除但不显示结果
[root@dns-server 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 ##删除以#开头的行

[root@dns-server 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 ##删除以#开头和结尾的

[root@dns-server 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
[root@dns-server 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开头的

[root@dns-server 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开头的

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

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

[root@dns-server mnt]# vim createuser.sh

在这里插入图片描述

[root@dns-server mnt]# sh createuser.sh
Error:Please input a userfile folling scripts!!
[root@dns-server mnt]# sh createuser.sh userfile
Error:userfile is not exists!!
[root@dns-server mnt]# vim userfile
[root@dns-server mnt]# cat userfile
westos1
westos2
westos3
[root@dns-server mnt]# sh createuser.sh userfile
westos1 is exist!!
westos2 is exist!!
westos3 is exist!!

3.a模式操作 ##添加

[root@dns-server 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模式操作 ##替换

[root@dns-server 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#
[root@dns-server 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文件中

[root@dns-server 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#
[root@dns-server mnt]# cat file
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

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

[root@dns-server mnt]# vim westos
[root@dns-server mnt]# cat westos
hahahaha
[root@dns-server 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模式操作 ##插入

[root@dns-server 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#
[root@dns-server 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所在的行

[root@dns-server mnt]# sed -n '/^UUID/=' fstab
9

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

[root@dns-server 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开头所在的行及行数

[root@dns-server 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

[root@dns-server mnt]# vim passwd
[root@dns-server 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
[root@dns-server 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

[root@dns-server 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

[root@dns-server 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对某文件的操作

[root@dns-server mnt]# vim file
[root@dns-server 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
[root@dns-server mnt]# cat file
/lp/,/halt/s/sbin/westos/g

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

[root@dns-server 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 ‘s@/@ @g’ passwd ##替换passwd中的/

[root@dns-server 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

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

[root@dns-server 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 ##除过最后一行,其余每行后面再加一行空行

[root@dns-server 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 ##在每一行的前一行显示行号

[root@dns-server 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’ ##每一行的前一行显示行号的同时,在每一行后添加空行

[root@dns-server 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’ ##在每一行行首显示行号

[root@dns-server 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 ##显示第一行

[root@dns-server mnt]# sed -n '1p' fstab

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

[root@dns-server mnt]# vim apache_port.sh

在这里插入图片描述

[root@dns-server mnt]# sh apache_port.sh 
ERROR:Apache is not running!!
[root@dns-server mnt]# systemctl start httpd
[root@dns-server mnt]# sh apache_port.sh 
ERROR:Please input port number folling scripts!!
[root@dns-server mnt]# sh apache_port.sh 22
ERROR:22 port is in used!!
[root@dns-server mnt]# sh apache_port.sh 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      0          79906      7068/httpd          
[root@dns-server mnt]# sh apache_port.sh 8888
tcp6       0      0 :::8888                 :::*                    LISTEN      0          80659      7130/httpd   
发布了108 篇原创文章 · 获赞 12 · 访问量 8386
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览