如何在文本的特定行的末尾附加内容 [英] How to append something at the end of a certain line of the text

查看:90
本文介绍了如何在文本的特定行的末尾附加内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些行的末尾添加一些内容(具有给定的字符). 例如,文本为:

I want to append something at the end of a certain line(have some given character). For example, the text is:

Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem
Line3:  How to solve the problem?
Line4:  Thanks to all.

然后我要在

Line2:  Thanks to all who look into my problem

"Line2"是关键字. (也就是说,我必须通过grep在此行中按关键字附加一些内容).

And "Line2" is the key word. (that is I have to append something by grep this line by key word).

因此脚本后的文本应为:

So the text after the script should be:

Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem Please help me
Line3:  How to solve the problem?
Line4:  Thanks to all.

我知道sed可以将某些内容追加到某些行,但是,如果使用sed '/Line2/a\Please help me',它将在该行后插入新行.那不是我想要的.我希望它附加到当前行.

I know sed can append something to certain line but, if I use sed '/Line2/a\Please help me', it will insert a new line after the line. That is not what I want. I want it to append to the current line.

有人可以帮我吗?

非常感谢!

推荐答案

我可能会去寻求John的sed解决方案,但是,由于您也询问了awk:

I'd probably go for John's sed solution but, since you asked about awk as well:

$ echo 'Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem
Line3:  How to solve the problem?
Line4:  Thanks to all.' | awk '/^Line2:/{$0=$0" Please help me"}{print}'

这将输出:

Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem Please help me
Line3:  How to solve the problem?
Line4:  Thanks to all.

有关其工作原理的说明可能会有所帮助.想到awk脚本,其条件如下,左边为条件,右边为命令.

An explanation as to how it works may be helpful. Think of the awk script as follows with conditions on the left and commands on the right:

/^Line2:/ {$0=$0" Please help me"}
          {print}

这两个awk子句在处理的每一行中都执行.

These two awk clauses are executed for every single line processed.

如果该行与正则表达式^Line2:匹配(在该行的开头表示"Line2:"),则可以通过添加所需的字符串来更改$0($0是读入).

If the line matches the regular expression ^Line2: (meaning "Line2:" at the start of the line), you change $0 by appending your desired string ($0 is the entire line as read into awk).

如果该行匹配空条件(所有行都将匹配此条件),则执行print.这将输出当前行$0.

If the line matches the empty condition (all lines will match this), print is executed. This outputs the current line $0.

因此,您可以看到它只是一个简单的程序,该程序可以在必要时修改行并输出行,无论是否修改.

So you can see it's just a simple program which modifies the line where necessary and outputs the line, modified or not.

此外,即使对于sed解决方案,您可能也想使用/^Line2:/作为键,这样您就不会在文本中间或Line20Line29中选择Line2Line200Line299,依此类推:

In addition, you may want to use /^Line2:/ as the key even for a sed solution so you don't pick up Line2 in the middle of the text or Line20 through Line29, Line200 through Line299 and so on:

sed '/^Line2:/s/$/ Please help me/'

这篇关于如何在文本的特定行的末尾附加内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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