简体   繁体   English

找到与sed匹配后如何追加一行?

[英]How to append just one line after found match with sed?

I want to append 'maildomain' after the line port in a php file. 我想在php文件中的行port后附加'maildomain' I really want to use this solution as this seems the cleanest and very clear so I did 我真的想要使用这个解决方案,因为这似乎是最干净,非常清楚,所以我做了

sed -i "350,/port/a \'maildomain\'" file.php

but it came out like this : 但它出来是这样的:

351 : 'maildomain'
352 : 
353 : 'maildomain'
354 : 
...
358 : blablabla port blabla
359 : 'maildomain'

As you can see, it adds 'maildomain' alternately from line 350 until it found port which is located at line 354 initially. 如您所见,它从第350行交替添加'maildomain' ,直到找到最初位于第354行的port How do I modify the sed command above to just add 'maildomain' after port and not before it? 如何修改上面的sed命令只是在port之后而不是之前添加'maildomain' Thank you. 谢谢。

Also, when use line 353 instead of 350, it gives me this : 另外,当使用353而不是350时,它给了我:

353 : 'maildomain'
354 : balsdbflasdbflsd port blablablabla
355 : 'maildomain'

Why? 为什么? Thanks~ 谢谢〜

An address of the form x,y means all the lines from x through y inclusive, so 350,/port/ means each line from 350 through the line that matches the /port/ regexp. 形式x,y的地址表示从xy所有行,包括350,/port/表示从350到与/port/ regexp匹配的行的每一行。

If you only want to append after the /port/ line, just specify that address: 如果您只想在/port/行后追加,只需指定该地址:

sed -i "/port/a \'maildomain\'" file.php

If you wish to add 'maildomain' after any line that contains port then Barmar has the right answer. 如果你想在任何包含port行之后添加'maildomain' ,那么Barmar有正确的答案。 However, if you have many such lines that contains port and only wish to add after line number 350 then you can do the following: 但是,如果您有许多这样的行包含port并且只希望在行号350之后添加,那么您可以执行以下操作:

sed -i "350,/port/{
    /port/a \'maildomain\'
}" file.php

This will look for lines after line number 350 and any line that has port in it, it will append 'maildomain' after it. 这将查找第350行之后的行以及其中包含port任何行,它将在其后附加'maildomain' Notice how I have split the command on multiple lines. 注意我是如何在多行上拆分命令的。 You need to do that as well. 你也需要这样做。 It is due to the fact that the a command in sed requires an actual newline character. 这是因为sed中的a命令需要一个实际的换行符。

If you only wish to add it once after line number 350 then you can branch out b after you are done adding. 如果你只是想后的行号350一次添加它,那么你可以另辟蹊径b在添加完之后。

sed -i "350,/port/{
    /port/a \'maildomain\'
    b;
}" file.php

If I'm understanding your requirements properly, you can do this in sed using Barmar's solution. 如果我正确理解您的要求,您可以使用Barmar的解决方案在sed中完成此操作。

Or you could use awk: 或者你可以使用awk:

awk '1;/port/{print "maildomain"}'

This prints the current line, and then tests to see if the current line includes your keyword, and if it does, prints the extra line. 这将打印当前行,然后测试当前行是否包含您的关键字,如果是,则打印额外行。

Or, since you've mentioned the bash tag, you could do this in pure bash: 或者,既然你已经提到了bash标签,你可以用纯粹的bash做到这一点:

while read line; do echo "$line"; [[ $line =~ port ]] && echo "maildomain"; done

This implements exactly the same logic as the awk solution, but without launching an external interpreter. 这实现了与awk解决方案完全相同的逻辑,但是没有启动外部解释器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM