成功的使用sed编辑器关键在于了解大量的命令和格式,这都有助于自定义文本编辑。
2.1、更多替换选项
前面已经看到过使用s命令实现新文本替换某一行中的文本,这里还有其他几个选项实现替换命令
2.1.1、替换标记
[root@www.linuxidc.com ~]# cat test2
this is a test of test file
This is the second test of the test file
[root@www.linuxidc.com ~]# sed 's/test/trial/' test2
this is a trial of test file
This is the second trial of the test file
从上面可以看出,替换命令在默认情况下仅仅替换各行中首次出现的文本,如果要实现继续替换后续文本,就必须使用替换标记,其格式为:
s/xxx/xxx/flags
这里头的flags有如下4种:
* 数字:表示新文本替换的模式
* g:表示用新文本替换现有的所有实例
* p:表示打印原始行的内容
* w file:将替换行的结果写入文件
所以向上面指定替换第二行的匹配模式则为:
[root@www.linuxidc.com ~]# sed 's/test/trial/2' test2
this is a test of trial file
This is the second test of the trial file
如果要替换所有匹配的模式:
[root@www.linuxidc.com ~]# sed 's/test/trial/g' test2
this is a trial of trial file
This is the second trial of the trial file
对于p替换标记会打印包含替换命令中匹配模式的那一行
[root@www.linuxidc.com ~]# cat test3
this is a test line
This is a different line
[root@www.linuxidc.com ~]# sed 's/test/trial/p' test3
this is a trial line
this is a trial line
This is a different line
p标记会输出所有已修改的行,但同时也会使得不匹配的行也print出来,所以可以通过-n选项禁止不匹配的行输出:
[root@www.linuxidc.com ~]# sed -n 's/test/trial/p' test3
this is a trial line
如上的这一行就是替换后生成的输出文本,当然我们还可以使用w选项保存到指定的文本中去:
[root@www.linuxidc.com ~]# sed -n 's/test/trial/w writefile' test3
表示执行替换匹配行后把匹配替换的那一行保存到writefile中,该文件是在保存的过程中自动创建:
[root@www.linuxidc.com ~]# cat writefile
this is a trial line
如果上面不使用-n选项,那么sed编辑器的正常输出将出现在STDOUT中,而匹配模式的那些行就保存到文件。
2.1.2、替换字符
有时候会在文本字符中遇到不容易替换的字符,如正斜杠/
这个时候就需要使用反斜杠使其转义,所以这挺复杂的,比如修改用户的shell类型:
[root@www.linuxidc.com ~]# cat /etc/passwd | sed 's/\/bin\/bash/\/bin\/csh/' | more
root:x:0:0:root:/root:/bin/csh
......
因为正斜杠用作字符串定界符,所以如果正斜杠出现在模式文本中,需要使用反斜杠进行转义。
但由于这种方式很很容易导致混淆或者错误,sed编辑器允许为替换命令中的字符而选择一个不同的字符:
[root@www.linuxidc.com ~]# cat /etc/passwd | sed 's!/bin/bash!/bin/csh/!' | more
root:x:0:0:root:/root:/bin/csh/
如上使用感叹号用作字符串定界符,从而使得阅读和理解路径名更为容易。
2.2、使用地址
默认情况下,在sed编辑器中使用的命令应用于所有文本数据行。如果仅想将某个命令应用于某一特定的文本数据行或者一组文本数据行,则必须使用行寻址。
在sed编辑器中,行寻址有两种形式:
* 行的数值范围
* 筛选行的文本模式
2.2.1、数字式行寻址
使用数字式行寻址,可以使用行在文本流中的位置引用行。sed编辑器指定文本流中的首行行号为1,每一个新行延续加1。当然,命令中可以是单个行号,也可以是由起始行号、逗号和结束行号指定范围,如下:
[root@www.linuxidc.com ~]# cat test4
my profession is something about linux
my profession is something about linux
my profession is something about linux
[root@www.linuxidc.com ~]# sed '2s/linux/unix/' test4
my profession is something about linux
my profession is something about unix
my profession is something about linux
如上将第二行中出现linux的匹配行进行替换为unix
[root@www.linuxidc.com ~]# sed '2,3s/linux/unix/' test4
my profession is something about linux
my profession is something about unix
my profession is something about unix
把第二行到第三行中匹配的文本进行替换
[root@www.linuxidc.com ~]# sed '2,$s/profession/hobby/' test4
my profession is something about linux
my hobby is something about linux
my hobby is something about linux
把第二行到最后一行中匹配的文本进行替换,所以这里可以借用美元符号。
2.2.2、使用文本模式筛选器
限制某个命令应用于哪些行的另一种方法如下:
[root@www.linuxidc.com ~]# tail -1 /etc/passwd | sed '/MySQL/s!/sbin/nologin!/bin/login!'
mysql:x:27:27:MySQL Server:/home/mysql:/bin/login
对于出现mysql的匹配行,把/sbin/nologin修改成/bin/login
2.2.3、组合命令
如果需要要单独一行上执行多个命令,可以使用大括号将命令组合一起:
[root@www.linuxidc.com ~]# sed '2{
> s/profession/hobby/
> s/linux/unix/
> }' test4
my profession is something about linux
my hobby is something about unix
my profession is something about linux
2.3、删除行
要知道,替换命令不是sed编辑器唯一命令,如果要删除文本流中特定的文本行,可以使用d选项,只要包含匹配模式,所有文本都将会从文本流中删除,如下:
[root@www.linuxidc.com ~]# cat test5
this is number 1
this is number 2
this is number 3
this is number 4
[root@www.linuxidc.com ~]# sed 'd' test5
所以我们使用删除命令时可以按照指定的范围执行:
[root@www.linuxidc.com ~]# sed '2d' test5
this is number 1
this is number 3
this is number 4
这样就实现删除了第二行的文本,同样可以借用美元符号删除:
[root@www.linuxidc.com ~]# sed '3,$d' test5
this is number 1
this is number 2
表示从第三行到最后一行都删除。
当然,sed编辑器的匹配模式功能也适用于删除命令:
[root@www.linuxidc.com ~]# sed '/number 3/d' test5
this is number 1
this is number 2
this is number 4
表示含有number 3的文本行给删除掉。
注意:如上所有的删除操作不会处理原始文件,所删除的仅仅是输出到STDOUT的内容:
[root@www.linuxidc.com ~]# cat test5
this is number 1
this is number 2
this is number 3
this is number 4
我们可以看到test5被处理后的完整性!
2.4、插入和附加文本
sed编辑器允许向数据流插入和附加文本,两个操作的差别如下:
* 插入命令(i)在指定行之前添加新的一行
* 附加命令(a)在指定行之后添加新的一行
下面看一个例子就很好理解了:
[root@www.linuxidc.com ~]# cat test5
this is number 1
this is number 2
this is number 3
this is number 4
[root@www.linuxidc.com ~]# sed '3i\
> this is an insert number' test5
this is number 1
this is number 2
this is an insert number
this is number 3
this is number 4
在指定的第三行之前添加一行
[root@www.linuxidc.com ~]# sed '3a\
> this is an insert number' test5
this is number 1
this is number 2
this is number 3
this is an insert number
this is number 4
在指定的第三行之后添加一行(把a理解为after的首字母即可)
如果要在最前面添加新的一行,那么通过1i即可实现,如果是附加多行呢?那么在每一个新行之后使用一个反斜杠即可,直到到达要添加文本的最后一行:
[root@www.linuxidc.com ~]# sed '1i\
> this is first insert number\
> this is second insert number ' test5
this is first insert number
this is second insert number
this is number 1
this is number 2
this is number 3
this is number 4
这样子就把两行都添加到数据流中了。
