25

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

</a><a><a>

</a>
<a><a>

I did this

sed 's#</a><a><a>#</a>\n<a><a>#g' filename but it didn't work.

jww
  • 90,984
  • 81
  • 374
  • 818
codereviewanskquestions
  • 12,620
  • 25
  • 97
  • 162

3 Answers3

36

Powered by mac in two Interpretation:

  1. echo foo | sed 's/f/f\'$'\n/'
  2. echo foo | gsed 's/f/f\n/g'
Max
  • 361
  • 1
  • 3
  • 2
22

Some seds, notably Mac / BSD, don't interpret \n as a newline, you need to use an actual newline, 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/'
Iulian Onofrei
  • 8,409
  • 9
  • 67
  • 106
Kevin
  • 51,293
  • 15
  • 96
  • 128
0

...or you just pound on it! worked for me on insert on mac / osx:

  sed "2 i \\\n${TEXT}\n\n" -i ${FILE_PATH_NAME}
  sed "2 i \\\nSomeText\n\n" -i textfile.txt
muet
  • 41
  • 4
  • 1
    Can you give more information about the mac osx version? I have this output `sed: 1: "2 i \\nSomeText\n\n": extra characters after \ at the end of i command` – Yohan W. Dunon Jul 05 '21 at 13:04