这是示例文件:
somestuff...
all: thing otherthing
some other stuff
我想要做的是像这样添加到以 all:
开头的行:
somestuff...
all: thing otherthing anotherthing
some other stuff
最佳答案
这对我有用
sed '/^all:/ s/$/ anotherthing/' file
第一部分是要查找的模式,第二部分是普通 sed 的替换,使用 $
作为行尾。
如果你想在这个过程中改变文件,使用-i
选项
sed -i '/^all:/ s/$/ anotherthing/' file
或者你可以将它重定向到另一个文件
sed '/^all:/ s/$/ anotherthing/' file > output
关于bash - 如何使用 sed 或 awk 添加到包含模式的行的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591744/