98

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
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
yasar
  • 13,158
  • 28
  • 95
  • 160

6 Answers6

187

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
user219882
  • 15,274
  • 23
  • 93
  • 138
  • Just adding something leads to a new line, replace is the right way. – zhy Nov 02 '18 at 08:34
  • 2
    On Mac OSX, for the `-i` option, I had to do like: `sed -i '' -e '/^all:/ s/$/ anotherthing/' file` – Mahdi Feb 03 '21 at 14:09
19

You can append the text to $0 in awk if it matches the condition:

awk '/^all:/ {$0=$0" anotherthing"} 1' file

Explanation

  • /patt/ {...} if the line matches the pattern given by patt, then perform the actions described within {}.
  • In this case: /^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.

Test

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
10

This should work for you

sed -e 's_^all: .*_& anotherthing_'

Using s command (substitute) you can search for a line which satisfies a regular expression. In the command above, & stands for the matched string.

izidor
  • 4,068
  • 5
  • 33
  • 43
9

Here is another simple solution using sed.

$ sed -i 's/all.*/& anotherthing/g' filename.txt

Explanation:

all.* means all lines started with 'all'.

& represent the match (ie the complete line that starts with 'all')

then sed replace the former with the later and appends the ' anotherthing' word

chenchuk
  • 5,324
  • 4
  • 34
  • 41
6

In bash:

while read -r line ; do
    [[ $line == all:* ]] && line+=" anotherthing"
    echo "$line"
done < filename
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
5

Solution with awk:

awk '{if ($1 ~ /^all/) print $0, "anotherthing"; else print $0}' file

Simply: if the row starts with all print the row plus "anotherthing", else print just the row.

Manlio
  • 10,768
  • 9
  • 50
  • 79
  • 4
    You can shorten that to: `awk '$1=="all:" {$(NF+1)="anotherthing"} 1'` – glenn jackman Mar 06 '12 at 21:12
  • 2
    @Prometheus, an awk script is composed of `condition {actions}` pairs. If the `condition` is omitted, the actions are performed for every record. If the `{actions}` are omitted, and the condition evaluates to *true* (which is the case for the number `1`) then the default action is to print the current record. – glenn jackman Mar 27 '13 at 00:35