The standard command-line tools, even awk, don't fully understand CSV. You may be able to fashion something that works properly with a more advanced language like python/perl/ruby/GO/etc.
If all you want to do is collapse split lines, the following hack might do:
awk -F, 'NF==4{print;next} NF<4{x=(x~/./?x" ":"") $0; if(split(x,a,FS)>=4){print x;x=""}}' input.csv
Or for easier reading:
BEGIN {
FS=OFS=","
}
NF==4 {
print
next
}
NF<4 {
x=(x ~ /./ ? x" " : "") $0
if (split(x,a,FS)>=4) {
print x
x=""
}
}
This will print-and-move-on if a line has 4 fields. For any smaller field count, it will begin concatenating lines until it sees at least 4 fields, then will print what it's collected. Note that the return value of the split()
function should be the item count of the array created by the split.