Sed 编辑器基础使用指南
1. 更多替换选项
在使用 sed 编辑器时, s 命令可用于替换行中的文本。不过,替换命令还有其他一些实用选项。
1.1 替换标志
替换命令在替换文本字符串中的匹配模式时存在一个默认规则:默认情况下,它只替换每行中的第一个匹配项。若要对不同位置的匹配项进行替换,就需要使用替换标志。替换标志的设置格式为: s/pattern/replacement/flags 。
以下是四种可用的替换标志:
- 数字 :指定要替换的匹配模式的出现位置。
- g :表示替换文本中所有出现的匹配模式。
- p :表示打印原始行的内容。
- w file :将替换结果写入指定文件。
下面通过具体例子来展示这些标志的用法:
# 查看 data4 文件内容
$ cat data4
This is a test of the test script.
This is the second test of the test script.
# 默认只替换每行第一个匹配项
$ sed 's/test/trial/' data4
This is a trial of the test script.
This is the second trial of the test script.
超级会员免费看
订阅专栏 解锁全文
1

被折叠的 条评论
为什么被折叠?



