0

I have the following csv file:

hostname1,this is a test,001^M
hostname2,this is
a test,002^M
hostname3,this
is
a
test,003^M

The first line is the proper format of the line. I want the other lines to follow the same format. The problem is for the second column, some of them have carriage returns so the third column is on a new line, but I want to use sed to join all the lines regardless of how many lines they broke apart.

1

Assuming the ^M sequences represent CR characters, i.e.

$ cat -et file.csv
hostname1,this is a test,001^M$
hostname2,this is$
a test,002^M$
hostname3,this$
is$
a$
test,003^M$

then you can join lines to the next if the do not end in CR using

sed -e :a -e '/\r$/!N; s/\n//; ta' file.csv

ex.

$ sed -e :a -e '/\r$/!N; s/\n//; ta' file.csv | cat -et
hostname1,this is a test,001^M$
hostname2,this isa test,002^M$
hostname3,thisisatest,003^M$
  • Thanks so much! This worked! – luigi11 Aug 1 '19 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.