在每第N个模式匹配后添加一个特定的行.AWK SED你得到的任何东西

use*_*594 0 shell search awk grep sed

我有这样的文件

Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now

Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now


Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now


Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now



Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now
Run Code Online (Sandbox Code Playgroud)

我想在"寻找一个小鸡"的第三场比赛后添加一条线等待

所以上面应该读.

Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now

Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now


Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
**wait** 
Do the best part now


Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now



Go to Vegas
Play the blackjack
Earn a  lucky wad
Find a hot chick
Do the best part now
Run Code Online (Sandbox Code Playgroud)

在每N个字符串的匹配后插入另一行(在这种情况下它的"等待")
如何才能做到最好我可以在每个模式匹配后添加一个新行,但我想在每个第N个模式匹配后添加一个特定的行这个

sed '/"Find a hot chick"/ a\
    wait ' <filename>
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 5

像这样:

awk '1;/Find a hot chick/{if(i++==3){print "New line";i=0}}' yourfile
Run Code Online (Sandbox Code Playgroud)

"1"只是为每行提供awk默认的东西,即打印.

  • 一个稍微紧凑的替代方案,避免`if`并在块之前保持条件*awk-style*:`awk'1; /找一个热门小鸡/ &&!(++ i%3){print"**wait**"}'file` (2认同)