This calls for a program rather than a quick command. The best way to do it would be, as suggested by Andrew Medico, to employ a proper CSV parser (in the case of perl
you have Text::CSV
).
However, I thought I'd write a perl
script that works in very simple cases:
perl -F, -lane 'if($.==1){@a=@F;next};for($i=0;$i<@F;$i++){if($F[$i]!=""){push @c,$F[$i];push @b,$i}}if(@a){foreach(@b){push @t,$a[$_]};print join(",",@t);undef @a}print join(",",@c);undef @c' file.csv
This saves the first line and goes on to see if there are any empty fields in the next line. It then prints only the relevant headers, skipping the empty field in all lines.
Please note that it doesn't handle commas inside quoted strings. It does, however, turn:
Test01,Test02,Test03,Test04
11,22,,44
11,22,,44
11,22,,44
11,22,,44
11,22,,44
11,22,,44
into:
Test01,Test02,Test04
11,22,44
11,22,44
11,22,44
11,22,44
11,22,44
11,22,44