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.
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 andudp
on the second line. If so append text to it.
awk '/vopied/{f=1}f&&!/vopied/{print "next line";f=0}1' file
精彩评论