sed 使用以下命令替换以特定模式开头的行中的单词 [英] sed replace a word at a line which begins with a specific pattern using
本文介绍了sed 使用以下命令替换以特定模式开头的行中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 FreeBSD 上替换以特定模式开头的行中的单词?考虑以下文件内容:
How can I replace a word at a line which begins with a specific pattern on FreeBSD? Consider the following file contents:
this is to test
that was for test
我想替换测试"在以this"开头的行.
I want to replace "test" at the line which begins with "this".
推荐答案
为了对以 this
开头的行执行替换,请说:
In order to perform a replacement for lines starting with this
, say:
$ sed '/^this/ s/test/something/' inputfile
this is to something
that was for test
这将在以 this
开头的行上用 something
替换单词 test
.
This would replace the word test
with something
on lines starting with this
.
如果要替换匹配行上的所有 test
实例,请为 sed 提供 g
选项:
If you want to replace all instances of test
on the matching lines, supply the g
option to sed:
sed '/^this/ s/test/something/g' inputfile
这篇关于sed 使用以下命令替换以特定模式开头的行中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文