I would like to delete all "\n"
(quotes, new line, quotes) in a text file.
I have tried:
sed 's/"\n"//g' < in > out
and also sed '/"\n"/d' < in > out
but non of those seds worked.
What am I doing wrong?
I would like to delete all "\n"
(quotes, new line, quotes) in a text file.
I have tried:
sed 's/"\n"//g' < in > out
and also sed '/"\n"/d' < in > out
but non of those seds worked.
What am I doing wrong?
This works with GNU sed on Linux: I don't have a Mac to test with.
sed '
# this reads the whole file into pattern space
:a; N; $ bb; ba; :b
# *now* make the replacement
s/"\n"//g
' <<END
one
two"
"three
four"
five
"six
END
one
twothree
four"
five
"six
This perl command accomplishes the same thing:
perl -0777 -pe 's/"\n"//g'
Try this -- you need to escape the backslash to make it literal.
sed 's/"\\n"//g' < in > out
Verified on OSX.
This awk-oneliner works here, you can give it a try:
awk -F'"\n"' -v RS='\0' -v ORS="" '{$1=$1;print}' file
a small test: tested with gawk
kent$ cat f
foo"
"bar"
"bla"
new line should be kept
this too
kent$ awk -F'"\n"' -v RS='\0' -v ORS="" '{$1=$1;print}' f
foo bar bla"
new line should be kept
this too
If you don't want to have the space between foo
and bar
blah
.., add -v OFS=""
to awk
The accepted answer was marked as such because of the Perl command it contains.
The sed
command doesn't actually work on OSX, because it uses features specific to GNU sed
, whereas OSX use BSD sed
.
An equivalent answer requires only a few tweaks - note that this will work with both BSD and GNU sed
:
Using multiple -e
options:
sed -e ':a' -e '$!{N;ba' -e '}; s/"\n"//g' < in > out
Or, using an ANSI C-quoted string in Bash:
sed $':a\n$!{N;ba\n}; s/"\\n"//g' < in > out
Or, using a multi-line string literal:
sed ':a
$!{N;ba
}; s/"\n"//g' < in > out
BSD sed
requires labels (e.g., :a
) and branching commands (e.g., b
) to be terminated with an actual newline (whereas in GNU sed
a ;
suffices), or, alternatively, for the script to be broken into multiple -e
options, with each part ending where a newline is required.
For a detailed discussion of the differences between GNU and BSD sed
, see https://stackoverflow.com/a/24276470/45375
$':a\n$!{N;ba\n}'
is a common sed
idiom for reading all input lines into the so-called pattern space (buffer on which (subsequent) commands operate):
:a
is a label that can be branched to$!
matches every line but the last
{N;ba\n}
keeps building the buffer by adding the current line (N
) to it, then branching back to label :a
to repeat the cycle.s/"\n"//g
) is performed on the entire buffer.As for why the OP's approach didn't work:
sed
reads files line by line by default, so by default it can only operate on one line at a time.
In order to be able to replace newline chars. - i.e., to operate across multiple lines - you must explicitly read multiple/all lines first, as above.
instead of sed you could also use tr, I've tested it and for me it worked
tr -d '"\\n"' < input.txt > output.txt