awk to identify empty fields in line

I am trying to use awk to identify and print out records in fields that are empty along with which line they are in. I hope the awk below is close, it runs but nothing results. Thank you :).

awk

awk -F'\t' 'FNR==NR ~ /^[ ]*$/ { print "NR is empty" }' file

file

123    GOOD    ID    45

122    GOOD    TEST    55
123    GOOD    ID
145
145    GOOD    id    39

desired output

line2 $1 $2 $3 $4 are empty
line4 $4 is empty
line5 $2 $3 $4 are empty
awk -F'\t'   NF==0 { print NR, " is empty"; next }
                 NF<4   { print 4-NF " missing fields}' file

Play with that cde

1 Like

Hello cmccabe,

Following may also help you in same.
i- If you want to give number of fields manually into a variable then following my help you in same.

awk -vR=4 '!NF{print "Line " NR is " is completly empty.";next} {for(i=1;i<=R;i++){if(!$i){Q=Q?Q OFS "$"i:"$"i;j++}};;w=j>1?" fields are empty.":" field is empty.";if(Q){print "In Line " NR FS Q w;Q=""}}' OFS=,  Input_file

Output will be as follows.

Line 2 is completly empty.
In Line 4 $4 field is empty.
In Line 5 $2,$3,$4 fields are empty

ii- If you want to take number of fields from very first line and it is not empty then following may help you in same.(considering 1st line will have all the fields and other all lines may not have here.)

awk 'NR==1{R=NF} !NF{print "Line " NR is " is completly empty.";next} {for(i=1;i<=R;i++){if(!$i){Q=Q?Q OFS "$"i:"$"i;j++}};;w=j>1?" fields are empty.":" field is empty.";if(Q){print "In Line " NR FS Q w;Q=""}}' OFS=,   Input_file

Output will be as follows.

Line 2 is completly empty.
In Line 4 $4 field is empty.
In Line 5 $2,$3,$4 fields are empty.

iii- In case your Input_file's fields are not fixed and they could be in any number into your Input_file then you could try following.

awk 'FNR==NR{R=R>NF?R:NF;next} !NF{print "Line " FNR is " is completly empty.";next} {for(i=1;i<=R;i++){if(!$i){Q=Q?Q OFS "$"i:"$"i;j++}};;w=j>1?" fields are empty.":" field is empty.";if(Q){print "In Line " FNR FS Q w;Q=""}}' OFS=,  Input_file  Input_file

EDIT: Adding non-one liner forms of solutions now.
1st code(where we are mentioning the number of field's value in variable R by ourselves.):

awk -vR=4 '!NF{
		print "Line " NR is " is completly empty.";
		next
              } 
              {
		for(i=1;i<=R;i++){
					if(!$i){
							Q=Q?Q OFS "$"i:"$"i;
							j++
                                               }
                                 };;
                w=j>1?" fields are empty.":" field is empty.";
                if(Q){
             		print "In Line " NR FS Q w;
			Q=""
		     }
              }
          ' OFS=,   Input_file

2nd code(Where we are setting number of fields values from first line, which we are considering can't be empty and number of fields later in Input_file may vary.):

awk  'NR==1   {
		R=NF;
              }
		
	   !NF{
		print "Line " NR is " is completly empty.";
		next
              } 
              {
		for(i=1;i<=R;i++){
					if(!$i){
							Q=Q?Q OFS "$"i:"$"i;
							j++
                                               }
                                 };;
                w=j>1?" fields are empty.":" field is empty.";
                if(Q){
             		print "In Line " NR FS Q w;
			Q=""
		     }
              }
          ' OFS=,   Input_file

3rd code(Considering number of fields may vary in whole Input_file, so first taking them by reading Input_file twice. When we have highest number of fields value then checking for that for all the lines in second time of Input_file reading.):

awk 'FNR==NR{
		R=R>NF?R:NF;
		next
            } 
     !NF    {
		print "Line " FNR is " is completly empty.";
		next
            } 
            {
		for(i=1;i<=R;i++){
					if(!$i){
							Q=Q?Q OFS "$"i:"$"i;
							j++
					       }
                                 };;
                w=j>1?" fields are empty.":" field is empty.";
                if(Q){
			print "In Line " FNR FS Q w;
			Q=""
		     }
            }
     ' OFS=,   Input_file  Input_file

Thanks,
R. Singh

1 Like

Thank you both for your help :slight_smile: