You can append the text to $0
in awk if it matches the condition:
awk '/^all:/ {$0=$0" anotherthing"} 1' file
/patt/ {...}
if the line matches the pattern given by patt
, then perform the actions described within {}
./^all:/ {$0=$0" anotherthing"}
if the line starts (represented by ^
) with all:
, then append anotherthing
to the line.1
as a true condition, triggers the default action of awk
: print the current line (print $0
). This will happen always, so it will either print the original line or the modified one.For your given input it returns:
somestuff...
all: thing otherthing anotherthing
some other stuff
Note you could also provide the text to append in a variable:
$ awk -v mytext=" EXTRA TEXT" '/^all:/ {$0=$0mytext} 1' file
somestuff...
all: thing otherthing EXTRA TEXT
some other stuff