【发布时间】:2013-09-12 16:50:34
【问题描述】:
如何在 FreeBSD 上替换以特定模式开头的行中的单词? 考虑以下文件内容:
this is to test
that was for test
我想在以“this”开头的行替换“test”。
【问题讨论】:
标签: sed
如何在 FreeBSD 上替换以特定模式开头的行中的单词? 考虑以下文件内容:
this is to test
that was for test
我想在以“this”开头的行替换“test”。
【问题讨论】:
标签: sed
要替换以this 开头的行,请说:
$ sed '/^this/ s/test/something/' inputfile
this is to something
that was for test
这将在以this 开头的行上用something 替换单词test。
如果要替换匹配行上的所有 test 实例,请向 sed 提供 g 选项:
sed '/^this/ s/test/something/g' inputfile
【讨论】:
要就地进行更改,请使用以下命令:
sed -i '/^this/ s/test/something/g' inputfile;
【讨论】: