接上篇,sed替换命令有四种替换标记
在第一种替换标记中,使用数字,可以指定替换文本行中字符出现的准确位置,例子如下:
- $ sed 's/test/study/2' test2.txt
- this is script test, we are study bash shell
- this is script test, we are study bash shell
- this is script test, we are study bash shell
- this is script test, we are study bash shell
- this is script test, we are study bash shell
此例子,是直接指定替换每行里面第二次出现的匹配字符,第一个匹配字符是不被替换的。这就是数字替换标记的妙用。
g替换标记能够替换文本模式中每一个出现的匹配字符。
- $ sed 's/test/study/g' test2.txt
- this is script study, we are study bash shell
- this is script study, we are study bash shell
- this is script study, we are study bash shell
- this is script study, we are study bash shell
- this is script study, we are study bash shell
与上述数字替换标记相比,g将所有出现的匹配字符全部替换为需要的新字符了。
p替换标记会打印包含替换命令中匹配模式的那一行,经常和-n选项一起使用。
示例如下:
- $ cat test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- $ sed -n 's/test/study/p' test2.txt
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
只有加-p的时候,才能打印匹配行,不加-p,执行命令后,不会有任何显示。
w替换标记生成相同的输出,但是会将输出保存到制定的文件中:
- $ sed 's/test/study/w test3' test2.txt
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- $ cat test3
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
看到了吧,加上w参数后,直接将替换输出的文本内容保存到新建的test3文本中了,很实用的替换标记。需要注意的是,w参数只将被替换的文本行输出到要保存的文本当中。
在文本字符串当中,我们还会遇到不容易在替换模式中实用的字符,在linux环境下最常见的就是正斜杠了。替换某个文件中的路径名,会觉得很困难。举个例子,要用cshell替换/etc/passwd文件中的bash shell,必须按照下面这样做:
- $ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd/
正斜杠被用作字符串定界符,若正斜杠出现在模式文本中,必须使用反斜杠使其转义,否则会导致错误和混淆。
要解决这个问题,也有一个办法,sed编辑器允许替换命令中的字符串定界符选择一个不同的字符:
- $ sed 's!/bin/bash!/bin/csh!' /etc/passwd
默认情况下sed编辑器使用的命令应用于文本中所有的数据行,如果我们只想改变文本但中的某一行或者一组数据行的话,那就要使用到行寻址。在sed编辑器当中,有两种行寻址的形式。
两种形式使用相同格式的指定地址:
- [address]command
同时,也能将多个命令组合在一起,应用到特定地址上:
- address {
- command1
- command2
- command3
- }
sed编辑器将指定的每一个命令仅用于指定地址匹配的行
第一种:数字寻址法
所谓数字寻址法,就是利用数字表示行号,利用sed编辑器可以修改掉指定的数字行号。在数字寻址当中,sed编辑器默认将文本中的第一行定义为数字1,接下来的行号延续下去。在命令中指定的地址可以是单个行号,也可以由起始行号、逗号和结束行号来指定一个寻址范围。看下面这个例子:
- $ sed '2s/study/test/' test2.txt
- this is script study, we are test bash shell
- this is script test, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
这个例子很明显,我们将文本中第二行利用数字2 寻址,将study替换为test了。
继续看下面这个例子,我们使用文本行数字范围进行寻址替换:
- $ sed '2,3s/study/test/' test2.txt
- this is script study, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
指定数字范围后,将指定的2,3两行的字符串替换为需要的字符串了,真的很方便。
如果要将命令应用于文本行内某一点开始直到文本结束的一组文本行,那就需要用到特殊符号—美元符号$
- $ sed '2,$s/study/test/' test2.txt
- this is script study, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
$在这里发挥了最佳的数字寻址作用!
第二种:使用文本模式筛选器
文本模式筛选器稍微有些复杂,命令格式为:
- /pattern/command
在使用文本筛选器的过程中,必须要使用正斜杠标记指定的pattern。sed编辑器只将该命令应用于指定的文本模式行。看下面这个例子,如果我们只想更改用户renyudi2默认的shell,可以使用如下方式:
- $ sed '/renyudi2/s/bash/csh/' /etc/passwd
- renyudi2:x:501:501::/home/renyudi2:/bin/csh
- mysql:x:502:502::/home/mysql:/bin/bash
- extmail:x:503:503::/home/extmail:/bin/bash
- postfix:x:504:504::/home/postfix:/bin/bash
- vmta:x:5000:5000::/var/vmta:/bin/bash
- sysadmin:x:5001:5001::/home/sysadmin:/bin/bash
sed编辑器有时候在处理文本字符多处的时候,就要用到组合命令了。组合命令需要将所用要执行的命令用大括号扩起来。这个时候,sed编辑器将处理地址行上列出的所有命令。实例如下:
- $ sed '2{
- > s/script/study/
- > s/test/learn/g
- > }' test2.txt
- this is script test, we are test bash shell
- this is study learn, we are learn bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
多个命令组合的效果相当不错哦,上述例子指定替换了第二行的两个字符,而且在替换/test/learn/的时候还加了g,替换了行中出现的全部匹配字符。组合命令超强吧!本节就先记录到此,下篇继续!