Search code examples
csvawksedtrsolaris-10

Join lines together that do not end in a comma


I have a CSV file produced by MS Excel. Where merged cells exist in the original spreadsheet, the original rows containing these become multi-line in the CSV output. I would like to join these back into one line.

I am looking for a Unix (Solaris 10) tool (sed/awk/tr etc) that will join all lines not ending in a comma to the next line, while leaving all lines that end in a comma untouched. The newline character at the end of lines not ending in a comma would be replaced with a space.

E.g. If the input file contains:

,Dilbert,
,,Wally,
Alice,
Asok9
Dogbert:
Catbert,
Ratbert,

Then the resulting output will be:

,Dilbert,
,,Wally,
Alice,
Asok9 Dogbert: Catbert,
Ratbert,

Thanks.


Solution

  • Here you go:

    awk '{printf "%s"(/,$/?RS:FS),$0}' file
    ,Dilbert,
    ,,Wally,
    Alice,
    Asok9 Dogbert: Catbert,
    Ratbert,
    

    If line ends with , use RS (Record Selector is default new line)
    If no, use FS (Field Separator is default one space)