23

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?

34

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
  • 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 '14 at 20:49
  • 2
    @Justin: sed 's/^Fred.*/& ***/' filename – Cyrus Oct 16 '14 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 '14 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.