tags:

views:

24

answers:

2

hi all

how to append 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

+1  A: 
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.

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