Insert Text after searching via grep

Hi Team,

I have a file with the following patterns:

 ==> xyz_Server_Started_with_Errors  <==
errors.

    ==> abc_Server_Started_with_Errors  <==
errors

  
==> reds_Server_Started_with_Errors  <==
errorss

I want them in this format:

===================================================
 ==> xyz_Server_Started_with_Errors  <==
===================================================

errors


===================================================

    ==> abc_Server_Started_with_Errors  <==
==================================================

errors


==================================================

  
==> reds_Server_Started_with_Errors  <==
==================================================

errorss


Thanks for the help :slight_smile:

What have you tried?

sed -i '/*Started_with_Errors/a =========================' one.txt > two.txt

But this doesnt seem to work

sed '/Started_with_Errors/{s/^/===================================================\
/;s/$/\
===================================================/;}' one.txt > two.txt
1 Like

Can you please quickly explain?

If a line contains pattern /Started_with_Errors/, then perform two substitution.

  1. Add "=======...\n" at the beginning of line containing pattern /Started_with_Errors/
s/^/===================================================\
/
  1. Add "\n=======..." at the end of the line containing pattern /Started_with_Errors/
s/$/\
===================================================/

---------- Post updated at 04:10 AM ---------- Previous update was at 03:37 AM ----------

sed '/Started_with_Errors/{ i\
===================================================
a\
===================================================
}' one.txt > two.txt