如何使用 sed 或 awk 添加到包含模式的行的末尾? [英] How to add to the end of lines containing a pattern with sed or awk?

查看:19
本文介绍了如何使用 sed 或 awk 添加到包含模式的行的末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例文件:

somestuff...
all: thing otherthing
some other stuff

我想要做的是添加到以 all: 开头的行,如下所示:

What I want to do is to add to the line that starts with all: like this:

somestuff...
all: thing otherthing anotherthing
some other stuff

推荐答案

这对我有用

sed '/^all:/ s/$/ anotherthing/' file

第一部分是要查找的模式,第二部分是使用 $ 作为行尾的普通 sed 替换.

The first part is a pattern to find and the second part is an ordinary sed's substitution using $ for the end of a line.

如果要在此过程中更改文件,请使用-i 选项

If you want to change the file during the process, use -i option

sed -i '/^all:/ s/$/ anotherthing/' file

或者你可以将它重定向到另一个文件

Or you can redirect it to another file

sed '/^all:/ s/$/ anotherthing/' file > output

这篇关于如何使用 sed 或 awk 添加到包含模式的行的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆