0

I have a CSV file with multiple columns, comma ", " separated and numbers of lines.
some lines have empty one or two fields "Columns". How I can identify in a separate file and/or remove those line with one or more empty columns with Awk command.

example

aaaa,bbbb,cccc,dddd,
,bbbb,cccc,dddd,
aaaa,,cccc,dddd,
aaaa,,,dddd,
,,,dddd,

The ", ," means empty columns. I tried this command but it did not work!

awk -F, '$1,4~/^$/' filename 

The expected output should be only :

aaaa, bbbb, cccc, dddd,

  • 1
    can you add expected output for the sample given? can you particularly clarify is trailing comma in a line mean empty field? – Sundeep Nov 24 '16 at 4:19
0

Can you just drop any line with two commas that are adjacent or separated only by whitespace, or that are first on the line? Something like:

 [dynamic<1>butlet]tmp $ cat data2
aaaa,bbbb,cccc,dddd,
,bbbb,cccc,dddd,
aaaa,,cccc,dddd,
aaaa,,,dddd,
,,,dddd,

 $ egrep -v ',[ ^I]*,|^[ ^I]*,' data2
aaaa,bbbb,cccc,dddd,
0
awk -F, '{for(i=1;i<=NF;i++)if($i==""){next}}1' inputfile

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.