File manipulation in awk

I have got a sample file below(colon(:slight_smile: is the field separator) . The data is like

col1:col2:col3:col4:col5:col6:col7:col8:col9:col10
11:12:012:aa:a a a:10::111:12:
311:321:320:caad::321:31:3333::
2:22:222::bbb::cads::2222:20
:::::12:1234::12:
:5:55::555:5555::::55550

Now I want to find the number of times the column is having no value (null).i.e. the output should be like
col1 - 2
col2 - 1
col3 - 1
col4 - 3
col5 - 2
col6 - 1
col7 - 2
col8 - 3
col9 - 2
col10 - 3

The file can have 'm' number of columns and can have 'n' number of records.

Try:

awk -F: 'NR==1{for (i=1;i<=NF;i++){a=$i}}NR>1{for (i=1;i<=NF;i++){if ($i==""){b++}}}END{for (i=1;i<=NF;i++){print a" - "b}}' file

Another approach:

awk -F: '{for(i=1;i<=NF;i++) a+=$i?0:1}
END{for (i=1;i<=NF;i++) print "col" i " - " a}
' file

Your approach is OK if the names of the columns in target file are "col1, col2.. etc". I think OP will have different column names :slight_smile:

In that case:

awk -F: 'NR==1{for(i=1;i<=NF;i++) c=$i}
{for(i=1;i<=NF;i++) a+=$i?0:1}
END{for (i=1;i<=NF;i++) print c " - " a}
' file

Well, I've found another issue with your code :D. It will misbehave if some field contains "0" - it is counting it as an empty field :stuck_out_tongue:

Same idea...

awk -F: 'NR==1{split($0,H);next} {for(i in H)if($i==x)A++} END{for(i=1;i<=NF;i++)print H" - "A}' infile

:eek: Thanks, here we go again:

awk -F: 'NR==1{for(i=1;i<=NF;i++) c=$i}
{for(i=1;i<=NF;i++) a+=$i==""?1:0}
END{for (i=1;i<=NF;i++) print c " - " a}
' file
""?1:0

Awesome. Thanks a lot to all of you for your quick response and test cases. It really helped me a lot. Thanking you once again.

Rinku