35

I have a line that says...

Fred Flintstone, Bedrock USA

and I want it to look like...

Fred Flintstone, Bedrock USA ***

How do I append a few * to the end of the line using sed command?

1 Answer 1

49

You can use this:

sed 's/$/ ***/' filename

If you want to search for a specific string before appending to a line (that is you don't want it appended to EVERY line like the above command) you can use the following, this finds Fred Flintstone anywhere in a line, put^ in front if you only want to match the beginning of the line.

sed '/Fred Flintstone/ s/$/ ***/' filename
3
  • 1
    That worked perfectly! How do you use it to append to a certain line? The above command does it for every line in the text file. I'm interested in making in work for all lines that start with the name "Fred"
    – Justin
    Oct 16, 2014 at 20:49
  • 5
    @Justin: sed 's/^Fred.*/& ***/' filename
    – Cyrus
    Oct 16, 2014 at 20:53
  • @heemayl: "The replacement may contain the special character & to refer to that portion of the pattern space which matched" -- from man bash
    – Cyrus
    Oct 16, 2014 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .