1 替换标记
cat data4.txt
cat data4.txt This is a test of the test script. This is the second test of the test script.s/pattern/replacement/flags
有4中可用的替换标记:
# 数字,表名新文本将替换地基础模式匹配的地方
#g, 表名新文本将会替换所有匹配的文本;
#p,表名原先行的内容要打印出来
#w file,将替换的结果写入到文件
第一类替换
将标记替换指定为2的结果就是:sed编辑器只替换每行中第二次出行的匹配模式。
sed 's/test/trial/2' data4.txt This is a test of the trial script. This is the second test of the trial script.第二类替换:
g标记使你能替换文本中匹配模式所匹配过的每处地方
sed 's/test/trial/g' data4.txt This is a trial of the trial script. This is the second trial of the trial script.第三类替换:
-p选项通常和-n选项一起使用 -n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是只输出被替换命令修改过的行
cat data5.txt This is a test line. This is a different line. sed -n 's/test/trial/p' data5.txt This is a trial line.第四类替换
-w 替换标记会产生同样的输出,不过会将输出保存到指定文件中。
sed 's/test/trial/w test.txt' data5.txt This is a trial line. This is a different line. [root@LNMP01 ~]# cat test.txt This is a trial line.
