Skip to content
Advertisement

how to add @ at the first byte of the line after matching line pattern

I need help from expert on this. How can I add @ at the first byte of the line after found matching pattern. For example as below :

Input:

TESTING
1234
0001
0002
0003

Output:

TESTING
@1234
0001
0002
0003

Advertisement

Answer

You may execute the following sed script on the file:

sed -n '/TESTING/{p;n;s/^/@/;};p;' file

  • -n – don’t print output on default
  • /TESTING/ – if TESTING is matched
  • { – then
  • p; – print output, ‘TESTING’
  • n; – read next line, the line after TESTING
  • s/^/@/ – substitude beginning of the line wiht @
  • };
  • p; – print output. This will print all non-matched lines with TESTING or will print the next line after TESTING witch substituted ‘@’
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement