0

I cannot figure out how to replace \n in text with new lines using sed (sed) or awk (gawk). For example, I would like to have the sed or awk command replace

hello world\nI am from Mars\n

by

hello world

I am from Mars

Community
  • 1
  • 1
gammarayon
  • 92
  • 3
  • 9
  • 2
    Probably a duplicate of https://stackoverflow.com/questions/24574489/replace-n-with-newline-in-awk – KamilCuk Nov 04 '19 at 16:51

2 Answers2

0

I found the answer at https://unix.stackexchange.com/questions/140763/replace-n-by-a-newline-in-sed-portably:

awk '{gsub("\\\\n","\n")};1' filename
gammarayon
  • 92
  • 3
  • 9
  • If you use proper regexp delimiters you won't need so many backslashes. See https://unix.stackexchange.com/questions/140763/replace-n-by-a-newline-in-sed-portably#comment227806_140816. – Ed Morton Nov 04 '19 at 16:45
0

With GNU sed:

sed 's/\\n/\n/g' file

With bash parameter expansion:

var='hello world\nI am from Mars\n'
echo "${var//\\n/$'\n'}"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111