Monday, 19 August 2013

linux shell: prepend or append text to next line after matching line

linux shell: prepend or append text to next line after matching line

Using a shell script in Linux (bash or csh). I would like to find all
lines that match a given text pattern, and for each line prepend or append
(depending on the application) text to the following line.
For example, for a file containing lines like this:
HEADER 1
12345
HEADER 2
12345
HEADER 1
12345
HEADER 2
12345
If I am searching for "HEADER 1", and would like to append the text
"ABCDE", the output would be this:
HEADER 1
12345 ABCDE
HEADER 2
12345
HEADER 1
12345 ABCDE
HEADER 2
12345
I also have cases where I would want the ABCDE preceding 12345.
I have been trying to understand the usage of sed for this purpose, but I
feel stymied. Any pointers would be appreciated.

No comments:

Post a Comment