Sponsored Content
Top Forums Shell Programming and Scripting awk to identify empty fields in line Post 302980867 by RavinderSingh13 on Sunday 4th of September 2016 01:18:11 AM
Old 09-04-2016
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.
Code:
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.
Code:
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.)
Code:
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.
Code:
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.
Code:
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.):
Code:
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.):
Code:
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.):
Code:
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

Last edited by RavinderSingh13; 09-04-2016 at 02:43 AM.. Reason: Adding non-one liner forms of solutions now.
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

identify the empty directories

Hi Wrote the below script to identify the empty directories ,when executing the below showing that directory is not empty but the directories are empty.Please help to identify the empty directories 33 is not empty 33 is not empty 33 is not empty 33 is not empty for file in `find .... (5 Replies)
Discussion started by: mohan705
5 Replies

2. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

3. Shell Programming and Scripting

use awk to replace empty fields with the latest nonempty field

Hi suppose I have a csv file like this count,1977,1978,1979 usa, , , blue japan, red, yellow,green india, , yellow,blue china, blue, yellow, green I want the output to be(replace everything, including empty data, with the most recent data): ... (1 Reply)
Discussion started by: grossgermany
1 Replies

4. Shell Programming and Scripting

awk ignores fields with only spaces or empty

Hi, Does any one know how to avoid the scenario where awk ignores the fields having only spaces or empty fields? for instance, Data: "a","b","c","d",""," " code: awk -F, '{ print NF }' File the output I get is 4 instead of 6 do you know how to avoid this? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

5. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

6. Shell Programming and Scripting

awk script creates empty line

I have the following awk script to which I pass the file > 10 0 0 10 0 0 > 12.997 0 5.71132 12.9098 0.0687626 5.48855 12.7506 0.174324 5.13225 12.5913 0.262662 4.80643 12.4316 0.335652 4.50283 12.2717 0.394598 4.21542 12.1113 0.440399 3.93957 11.9506 0.473646 3.67148 11.7894... (2 Replies)
Discussion started by: kristinu
2 Replies

7. Shell Programming and Scripting

awk :quick question removing empty line.

How to write in awk to remove lines starting with "#" and then process the file: This is not working: cat file|awk '{if ($0 ~ /^#/) $0="";print NF>0}' When I just give cat file|awk '{if ($0 ~ /^#/) $0="";print }' it prints the blank lines . I don't wnat the blank lines along with the... (15 Replies)
Discussion started by: rveri
15 Replies

8. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

9. Shell Programming and Scripting

Identify empty file with null record

Hi Team, I have a file abc.dat which is a empty file. But it has null record in first line. I need to identify this unique file and handle it separately. scenario 1: abc/dw> wc abc.dat 1 0 1 abc.dat abc/dw> cat abc.dat abc/dw> scenario 2: abc/dw> wc pqr.dat 0 0 0 pqr.dat... (3 Replies)
Discussion started by: kmanivan82
3 Replies

10. Shell Programming and Scripting

awk empty fields

Hello I have a file like this a,b,c,1,2,3,d,e,f,,,,g,h,i,,,,j,k,l and using awk 'FS="'"{print $9,$10,$11}' does not work as I was hoping. I would like the empty fieds, i.e. between the two comma to be interpreted as a zero. is this possible? I would like to get f 0 0 out of the above... (1 Reply)
Discussion started by: garethsays
1 Replies
All times are GMT -4. The time now is 01:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy