Balthasar
Balthasar

Reputation: 1237

sed not substituting as expected

I need to substitute each \n in a line with "\n (double quote followed by newline).

This should work. But it does nothing. Reports no error either. Any clues anyone?

sed -i 's/\n/\"\n/' filename

before, the file contains:

line 1
line 2

after, it contains the exact same.

Thanks

Upvotes: 1

Views: 92

Answers (3)

Jotne
Jotne

Reputation: 41446

Using awk

awk '{$0=$0"\""}1' filename

Upvotes: 1

Barmar
Barmar

Reputation: 780724

A line can't contain \n, because \n is the delimiter between lines. sed operates on a single line at a time, and the newline is not included in it.

If you want to put a character before the end of each line, use the $ regexp:

sed -i 's/$/"/' filename

Upvotes: 5

falsetru
falsetru

Reputation: 368944

Try following:

sed -i 's/$/"/' filename

used $ to denote end of the line.

Upvotes: 2

Related Questions