0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3840048
  • 141
  • 1
  • 9
  • possible duplicate of [How can I replace a newline (\n) using sed?](http://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed) – NeronLeVelu Apr 01 '15 at 08:02
  • you probably want unix2dos, see http://stackoverflow.com/a/6373961/297323 – Fredrik Pihl Apr 01 '15 at 09:18
  • Do you mean " at the end of a line, a literal newline and a " on the beginning of the next line, or do you mean those four characters literally? – Fernando Basso Apr 01 '15 at 10:12
  • To clarify, since the representation of what should be replaced is confusing: based on what answer was accepted, the intent is to remove all 3-character substrings composed of: a literal double quote, followed by an actual newline, followed by a literal double quote. – mklement0 Apr 01 '15 at 21:38

5 Answers5

1

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'
mklement0
  • 382,024
  • 64
  • 607
  • 775
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • For those unfamiliar with Perl: `perl -0777` is an idiom for reading an input file _as a whole_ (all lines at once). – mklement0 Apr 01 '15 at 21:45
  • + for the Perl solution, and the _principle_ of the `sed` solution, which, however, as noted, _doesn't_ work on OSX; for a variant that _does_ work on OSX, see [my answer](http://stackoverflow.com/a/29404564/45375). – mklement0 Apr 02 '15 at 03:40
0

Try this -- you need to escape the backslash to make it literal.

sed 's/"\\n"//g' < in > out

Verified on OSX.

jstevenco
  • 2,913
  • 2
  • 25
  • 36
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
0

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

Kent
  • 189,393
  • 32
  • 233
  • 301
0

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.
  • Once the last line is reached, no branching is performed, and the buffer at that point contains all input lines, at which point the desired substitution (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.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

instead of sed you could also use tr, I've tested it and for me it worked

tr -d '"\\n"' < input.txt > output.txt
lrs_coolik
  • 81
  • 6