0

I want to replace the '\n' characters in each line with a longer string, e.g.: "___\n".

When I try:

$ cat /tmp/test.csv | sed ':a;N;$!ba;s/\n/____\n/g'

I get the following output

"00000";"29515470";"001";"001";"A6399000800";"";"";"-1";"142.72";"1.00";"1.00"____
"91930";"20029956";"001";"002";"A2128300018";"";"";"-1";"71.58";"1.00";"4.00"____
"91930";"20029962";"001";"003";"ZZ-OIL16";"";"";"-1";"14.48";"5.00";"3.00"____
"91930";"20029962";"001";"002";"A2661800009";"";"";"-1";"13.28";"1.00";"3.00"

Where the very last '\n' is not not replaced.

But my CSV file looks like this: enter image description here

How can I replace ALL \n characters?

I've found this: How can I replace a newline (\n) using sed? , but this solution is not good for me, because it does not handle the very last '\n' character.

elaspog
  • 1,635
  • 3
  • 21
  • 51
  • 2
    How about just `awk '{print $0"____"}' /tmp/test.csv` – Inian May 25 '18 at 15:25
  • Thank you! This is a good solution too.. But I have to use sed here... – elaspog May 25 '18 at 15:28
  • Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/).* – jww May 26 '18 at 01:32

2 Answers2

3

Using sed:

$ cat > foo
this
that
$ sed 's/$/__/g' foo
this__
that__

$ sed 's/$/__/g' foo | hexdump -C
00000000  74 68 69 73 5f 5f 0a 74  68 61 74 5f 5f 0a        |this__.that__.|

0a in the end.

PS. Notice the absence of the useless cat.

James Brown
  • 36,089
  • 7
  • 43
  • 59
2

I know you're not asking about Perl, but it's perfect for this —

perl -pe 's/$/____/' foo.csv
  • perl -pe runs the given code for every line of each given input file (or stdin), and then prints the resulting line. The code can modify the line before it is output.
  • In the code, s/$/____/ adds the four underscores at the end of the line.

Assuming all the input lines end with newlines, this will hit all the newlines. That is because the implicit loop of -p uses <>, which includes the newline in what your code gets.

The substitution works because $ matches before a newline that ends a string. DWIM!

Output:

"00000";"29515470";"001";"001";"A6399000800";"";"";"-1";"142.72";"1.00";"1.00"____
 "91930";"20029956";"001";"002";"A2128300018";"";"";"-1";"71.58";"1.00";"4.00"____
 "91930";"20029962";"001";"003";"ZZ-OIL16";"";"";"-1";"14.48";"5.00";"3.00"____
 "91930";"20029962";"001";"002";"A2661800009";"";"";"-1";"13.28";"1.00";"3.00"____

Sorry about the funky formatting — four-space didn't work for me for some reason.

Inian
  • 80,270
  • 14
  • 142
  • 161
cxw
  • 16,685
  • 2
  • 45
  • 81