I would like to add two lines after the first string search. I am using:
$ cat file1
HAI
BYE
HAI
ONE
TWO
$ VAR=`cat -n file1 |grep -w HAI |head -1 |awk '{print $1}'`
$ sed "$VAR a\
LINE ONE \
LINE TWO
" file1
It is giving the following output.
HAI
LINE ONE LINE TWO
BYE
HAI
ONE
TWO
But I want the output to be:
HAI
LINE ONE
LINE TWO
BYE
HAI
ONE
TWO
How can I achieve this? I tried to keep the \n but it is giving errors.
Replace your sed command with this:
btw your earlier grep, awk can also be reduced to this:
Much Better is to get full answer in single awk command like this:
OUTPUT: