How to add to the end of lines containing a pattern with sed or awk?

yasar picture yasar · Mar 6, 2012 · Viewed 152.4k times · Source

Here is example file:

somestuff...
all: thing otherthing
some other stuff

What I want to do is to add to the line that starts with all: like this:

somestuff...
all: thing otherthing anotherthing
some other stuff

Answer

user219882 picture user219882 · Mar 6, 2012

This works for me

sed '/^all:/ s/$/ anotherthing/' file

The first part is a pattern to find and the second part is an ordinary sed's substitution using $ for the end of a line.

If you want to change the file during the process, use -i option

sed -i '/^all:/ s/$/ anotherthing/' file

Or you can redirect it to another file

sed '/^all:/ s/$/ anotherthing/' file > output