Checking for null value

Dear All,

I'm glad that I've also joined in this forum. Good work fellas... :b:
Keep going...! :slight_smile:

I am having a .csv file where I am reading the values with comma(,) as delimiter. After reading each and every values, I would like to check whether it is empty (say null or blank spaces). How to do this using awk command?
Code:
awk 'BEGIN {FS=","}
{ n=split($0,col_value,",")
for(i=1;i<=n;i++) {
if(length(col_value[i]) == 0) {
BLANK_COUNT = BLANK_COUNT+1
}
}
} END {print BLANK_COUNT}' sample.csv

Input File (sample.csv):
RollNo,Name,Mark1,Mark2,Mark3,Mark4
1,Tom,78,,67,98,56
2,Steve,85, ,56,65,67
3,John,96, , ,98

My code is returning 1 blank space for rollno:1 only. Whereas my requirement is to calculate the blank count which includes spaces also.
i.e.,
Rollno, Blank
1,1
2,1
3,2

Thanks in advance...! :slight_smile:

You have a space in some fields, try this:

awk -F, '{
  for(i=1;i<=NF;i++){
    if($i==" " || $i==""){
      s++
    }
  }
{
  print NR, s;s=0}
}' file

Regards

It is working fine, if I have only one space. But in my input file, I might have more than single space say " " or " " or " " etc. But I dont know the exact number of spaces.

In that case:

awk -F, '{
  for(i=1;i<=NF;i++){
    gsub(" ","",$i)
    if($i==""){
      s++
    }
  }
{
  print NR, s;s=0}
}' file

Regards

awk -F"," '{j=0;for(i=1;i<=NF;i++){ if($i~/^[ ]+$/){j++}} print j;}' file