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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9591744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:44:42  来源:igfitidea点击:

How to add to the end of lines containing a pattern with sed or awk?

bashsedawk

提问by yasar

Here is example file:

这是示例文件:

somestuff...
all: thing otherthing
some other stuff

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

我想要做的是添加到以这样开头的行中all:

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

回答by user219882

This works for me

这对我有用

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

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.

第一部分是要查找的模式,第二部分是$用于行尾的普通 sed 替换。

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

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

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

Or you can redirect it to another file

或者您可以将其重定向到另一个文件

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

回答by izidor

This should work for you

这应该适合你

sed -e 's_^all: .*_& anotherthing_'

Using s command (substitute) you can search for a line which satisfies a regular expression. In the command above, &stands for the matched string.

使用 s 命令(替换),您可以搜索满足正则表达式的行。在上面的命令中,&代表匹配的字符串。

回答by fedorqui 'SO stop harming'

You can append the text to $0in awk if it matches the condition:

$0如果匹配条件,您可以将文本附加到awk 中:

awk '/^all:/ {
somestuff...
all: thing otherthing anotherthing
some other stuff
=
$ awk -v mytext=" EXTRA TEXT" '/^all:/ {
while read -r line ; do
    [[ $line == all:* ]] && line+=" anotherthing"
    echo "$line"
done < filename
=
$ sed -i 's/all.*/& anotherthing/g' filename.txt
mytext} 1' file somestuff... all: thing otherthing EXTRA TEXT some other stuff
" anotherthing"} 1' file

Explanation

解释

  • /patt/ {...}if the line matches the pattern given by patt, then perform the actions described within {}.
  • In this case: /^all:/ {$0=$0" anotherthing"}if the line starts (represented by ^) with all:, then append anotherthingto the line.
  • 1as a true condition, triggers the default action of awk: print the current line (print $0). This will happen always, so it will either print the original line or the modified one.
  • /patt/ {...}如果该行与 给出的模式匹配patt,则执行 中描述的操作{}
  • 在这种情况下:/^all:/ {$0=$0" anotherthing"}如果该行以 开头(由 表示^all:,则附加anotherthing到该行。
  • 1作为真条件,触发默认操作awk:打印当前行 ( print $0)。这将始终发生,因此它将打印原始行或修改后的行。

Test

测试

For your given input it returns:

对于您给定的输入,它返回:

awk '{if ( ~ /^all/) print ##代码##, "anotherthing"; else print ##代码##}' file

Note you could also provide the text to append in a variable:

请注意,您还可以提供要附加到变量中的文本:

##代码##

回答by glenn Hymanman

In bash:

在 bash 中:

##代码##

回答by chenchuk

Here is another simple solution using sed.

这是使用 sed 的另一个简单解决方案。

##代码##

Explanation:

解释:

all.* means all lines started with 'all'.

all.* 表示所有以“all”开头的行。

& represent the match (ie the complete line that starts with 'all')

& 代表匹配(即以'all'开头的完整行)

then sed replace the former with the later and appends the ' anotherthing' word

然后 sed 用后者替换前者并附加“另一个”词

回答by Manlio

Solution with awk:

awk 解决方法:

##代码##

Simply: if the row starts with allprint the row plus "anotherthing", else print just the row.

简单地说:如果该行以all打印该行加上“其他内容”开头,则仅打印该行。