sam h
sam h

Reputation: 113

sed help: matching and replacing a literal "\n" (not the newline)

i have a file which contains several instances of \n.

i would like to replace them with actual newlines, but sed doesn't recognize the \n.

i tried

sed -r -e 's/\n/\n/'
sed -r -e 's/\\n/\n/'
sed -r -e 's/[\n]/\n/'

and many other ways of escaping it.

is sed able to recognize a literal \n? if so, how?

is there another program that can read the file interpreting the \n's as real newlines?

Upvotes: 10

Views: 8868

Answers (5)

tripleee
tripleee

Reputation: 189936

What exactly works depends on your sed implementation. This is poorly specified in POSIX so you see all kinds of behaviors.

The -r option is also not part of the POSIX standard; but your script doesn't use any of the -r features, so let's just take it out. (For what it's worth, it changes the regex dialect supported in the match expression from POSIX "basic" to "extended" regular expressions; some sed variants have an -E option which does the same thing. In brief, things like capturing parentheses and repeating braces are "extended" features.)

On BSD platforms (including MacOS), you will generally want to backslash the literal newline, like this:

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

On some other systems, like Linux (also depending on the precise sed version installed -- some distros use GNU sed, others favor something more traditional, still others let you choose) you might be able to use a literal \n in the replacement string to represent an actual newline character; but again, this is nonstandard and thus not portable.

If you need a properly portable solution, probably go with Awk or (gasp) Perl.

perl -pe 's/\\n/\n/g' file

In case you don't have access to the manuals, the /g flag says to replace every occurrence on a line; the default behavior of the s/// command is to only replace the first match on every line.

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

  1. sed works one line at a time, so no \n on 1 line only (it's removed by sed at read time into buffer). You should use N, n or H,h to fill the buffer with more than one line, and then \n appears inside. Be careful, ^ and $ are no more end of line but end of string/buffer because of the \n inside.
  2. \n is recognized in the search pattern, not in the replace pattern. Two ways for using it (sample):
    sed s/\(\n\)bla/\1blabla\1/
    sed s/\nbla/\
    blabla\
    /
    
    The first uses a \n already inside as back reference (shorter code in replace pattern); the second use a real newline.

So basically

sed "N
$ s/\(\n\)/\1/g
"

works (but is a bit useless). I imagine that s/\(\n\)\n/\1/g is more like what you want.

Upvotes: 0

Bryan Newman
Bryan Newman

Reputation: 631

$ echo "\n" | sed -e 's/[\\][n]/hello/'

Upvotes: 0

Jotne
Jotne

Reputation: 41460

awk seems to handle this fine:

echo "test \n more data" | awk '{sub(/\\n/,"**")}1'
test ** more data

Here you need to escape the \ using \\

Upvotes: 0

Ashish
Ashish

Reputation: 1952

Can you please try this

sed -i 's/\\n/\n/g' input_filename

Upvotes: 12

Related Questions