开发者

sed + how to append some text by sed or awk after matching sequential two lines in file?

how to app开发者_JAVA技巧end some text by sed or awk after matching sequential two lines in file? remark (need to ignore empty lines if exist)

for example

we want to append the text "this is the new line" after the following match lines in file

   vopied  13783/tcp  # VOPIED Protocol
   vopied  13783/udp  # VOPIED Protocol

this is the new line

lidia


sed '
  /\/tcp/{
    N
    /.*\/tcp.*\n.*\/udp.*/a\
    this is the new line
  }
' yourfile

As in your other question, I shall elucidate the steps.

  1. Check for any line matching the tcp text and apply a series of commands on it:

    I. Get the new line in pattern space (where the strings to be matched are stored)

    II. Check if tcp is matched on the first line and udp on the second line. If so append text to it.


awk '/vopied/{f=1}f&&!/vopied/{print "next line";f=0}1' file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜