How to add a new line using sed on macOS?

I wanted to add a new line between </a> and <a><a>

 </a><a><a> </a> <a><a> 

I did it

sed 's#</a><a><a>#</a>\n<a><a>#g' filename but this did not work.

+15
source share
3 answers

Some sed , especially Mac / BSD, do not interpret \n as a newline, you need to use the actual newline, which is preceded by a backslash:

 $ echo foo | sed 's/f/f\n/' fnoo $ echo foo | sed 's/f/f\ > /' f oo $ 

Or you can use:

 echo foo | sed $'s/f/f\\\n/' 
+16
source

Runs on mac in two interpretations:

  • echo foo | sed 's/f/f\'$'\n/'
  • echo foo | gsed 's/f/f\n/g'
+29
source

... or just knock on it! worked for me on an insert on mac / osx:

  sed "2 i \\\n${TEXT}\n\n" -i ${FILE_PATH_NAME} sed "2 i \\\nSomeText\n\n" -i textfile.txt 
0
source

All Articles