0

The Unix sed command doesn't recognize a backslash escaped n \n as a newline character. For instance, the command sed "s!,!\n!g" is replacing commas in the input text with the letter n and not a newline character.

How can I make sed interpret \n as a newline character?

  • What shell are you using? – fd0 Oct 13 '15 at 19:20
4

If you are using bash, you can insert the newline character by hitting the enter key as

sed 's!,!\
!g'
  • That dint work either – hemanth Oct 13 '15 at 19:05
  • oops my appologies . This is working : – hemanth Oct 13 '15 at 19:35
2

sed "s/,/\n/g" works here, in bash

1

You may need to escape it with a second \

sed 's!,!\\n!g'

You may also need to replace out some white space, depending on what you are looking to do.

in my tests as well sed took issue to using "'s instead of ''s

  • Then it replaces a , with \n string not character. Not working either :( – hemanth Oct 13 '15 at 19:19
  • Hmm this worked for a redhat 6.7 machine, what is your environment? – Gravy Oct 13 '15 at 19:20
0

The Mac version of sed has this difficulty. Try installing the GNU version of sed with brew install coreutils and then

gsed "s/,/\\n/g"

should work. Alternatively, tr ',' \\n should work if the search string is only a single character.

0

Indeed Unix Echo + Sed not always process \n as new line character. I've solved it by providing carriage return ASCII 0xd with vertical tab ASCII 0xb instead.

For example add new line character with sed command using variable:

eol="\xd\xb"
echo -e "hello" | sed "s/$/$eol/"
  • This doesn't really address the question. – RalfFriedl Mar 19 at 9:58

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.