bash - 如何使用 sed 或 awk 添加到包含模式的行的末尾?

标签 bash sed awk

这是示例文件:

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/

相关文章:

bash - 如何将 ifconfig 与 openssl 结合使用以更改 MAC 地址

bash - 在变量赋值的 RHS 上引用与不引用变量

bash - Bash 中的 $@ 是什么?

perl - 批量重命名目录中的文件名

linux - 如何在 linux 中用不同的字符串替换相同的事件

bash - 别名在 Bash 脚本中不起作用

linux - 在 shell 脚本中使用 sed 替代不起作用

regex - 为什么在 Makefile 中使用 "$$"(两个美元符号)而不是 "$"(一个美元符号)来删除空行?

python - 使用awk根据某些行中的值过滤文本文件

linux - 如何使用 unix 在 XML 文件中的特定标记之间添加或替换特定字段