Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sed 'a' (append) doesn't work on OSX, and simply replaces line?

I’m trying to update a file by adding text to a new line after a matching line.
Following the documentation here I should be able to do this:

$ seq 3 | sed '2a hello'
1
2
hello
3

However, on Mac OSX, this doesn’t work. Firstly, because '2a hello' doesn’t seem to be interpreted as a valid sed command. Converting this to "s/2/a hello/" resolves the sed error, but simply replaces 2 with a hello:

$ seq 3 | sed "s/2/a hello/"
1
a hello
3

Is there some other way to use the append command with Sed on Mac?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

On OSX you should use multiline sed for this:

seq 3 | sed '2a\
hello
'

Output:

1
2
hello
3

PS: This command will work for gnu sed as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading