How to detect empty field in awk ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to detect empty field in awk ?
# 1  
Old 04-12-2013
How to detect empty field in awk ?

Hi ! programmers

I have a need of detecting empty field in file
my file looks like this

Code:
40.900|-71.600|1.6|20|1|1961|21.00|3.700||1|US|28035|10029370|31
40.900|-71.600|5.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31
40.900|-71.600|7.8|20|1|1961|21.00|3.700||1|US|28035|10029370|31
40.900|-71.600|8.5|20|1|1961|21.00|3.700||1|US|28035|10029370|31
40.900|-71.600|8.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31
-26.667|12.483|1.3|24|1|1961|20.00|16.530|35.059|4|ZA|3|8756280|80
-26.667|12.483|2.5|24|1|1961|20.00|16.530|35.061|4|ZA|3|8756280|80
-26.667|12.483|8.1|24|1|1961|20.00|16.530|35.060|4|ZA|3|8756280|80
40.900|-71.600|8.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31
-26.667|12.483|12.4|24|1|1961|20.00|16.530|35.059|4|ZA|3|8756280|80
-26.667|12.483|13.7|24|1|1961|20.00|16.250|35.035|4|ZA|3|8756280|80
-26.667|12.483|15.9|24|1|1961|20.00|15.860|34.982|4|ZA|3|8756280|80
-26.667|12.483|31.4|24|1|1961|20.00|13.020|34.925|4|ZA|3|8756280|80
-26.667|12.483|34.8|24|1|1961|20.00|13.000|34.999|4|ZA|3|8756280|80

in first column 9th column is empty, in another column 15 column may be empty...so like this I have many files
wherever field is empty I want to print string "BAD" in that location, I need solution for both FS="|" as well as mixed FS tab+space+no of empty spaces

expecting o/p like this
Code:
40.900|-71.600|1.6|20|1|1961|21.00|3.700|BAD|1|US|28035|10029370|31
40.900|-71.600|5.7|20|1|1961|21.00|3.700|BAD|1|US|28035|10029370|31
40.900|-71.600|7.8|20|1|1961|21.00|3.700|BAD|1|US|28035|10029370|31

Those who have solution please help. quick reply will be helpful

Last edited by Dona Clara; 04-12-2013 at 01:40 PM..
# 2  
Old 04-12-2013
This will work for FS="|"

Code:
awk -F"|" '{ for(N=1; N<=NF; N++) if($N=="") $N="BAD" } 1'

You'll need to show what you actually want for the other, that's more complicated than you've described here.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-12-2013
Also set the OFS (Output Field Separator) to preserve the field separator:
Code:
awk -F"|" '{ for(N=1; N<=NF; N++) if($N=="") $N="BAD" } 1' OFS="|" file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 04-12-2013
Thank you

both are working, but if suppose file is space separated, or tab separated or else mix of space,tab and |, what I need to do ?
and what does
Code:
}1'

mean ? please describe I am new to unix world
# 5  
Old 04-12-2013
To conserve space, programmers normally use '1' instead of '{print}' to print each line. Either one will work.

1 means true (1 == true), so the default action for awk is to print current record.
These 2 Users Gave Thanks to Yoda For This Post:
# 6  
Old 04-12-2013
If your fields are tab separated, you can use Yoda's solution if you change "|" both places where it appears in his script to "\t".
If fields are separated by a <space> or a mixture of <space> and <tab>, it isn't as easy to do it with awk because awk's default field separator (a space character) is interpreted to mean any combination of one or more space or tab characters.

If you really want to use a single space as your field separator, you could:
  1. look for a space at the start of a line and replace it with "BAD ",
  2. look for any occurrence of two spaces together and replace them with " BAD ", and
  3. look for a space at the end of a line and replace it with " BAD".

If you want to use every <space> or <tab> as a field separator, you could use the directions in the list above but use look for a space or tab every place where it currently says look for a space above and use look for any occurrence of a space or tab immediately followed by a space or tab where it currently says look for any occurrence of two spaces together. I will leave both of these options as an exercise for the reader.
# 7  
Old 04-12-2013
Quote:
Originally Posted by Dona Clara
Thank you

both are working, but if suppose file is space separated, or tab separated or else mix of space,tab and |, what I need to do ?
Depends what you want your output to look like, which I wondered before and am still wondering now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Web Development

Php help to copy form field if empty

I have an input form with several fields. What I would like to achieve is to auto populate or copy certain fields if they are empty when the form is submitted. I would like to use php if not then javascript but not jquery if possible - I have sort of had a go but I really have no idea... (4 Replies)
Discussion started by: barrydocks
4 Replies

3. Shell Programming and Scripting

print non empty column/field value

I have below file 25-09-2012 24-09-2012 19-09-2012 31-07-2012 30-04-2012 30-03-2012 ASIAEXFVNV N/A CEU 4 DMIRSOA N/A CAS 2 2 2 DMIRSOA N/A MIDMT 2 NFIAL22 N/A HVNY 11 11 11 NFIAL22 N/A NYAL3 11 11 11 NFIAL22 N/A NYCN 11 11 11 ... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

4. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

5. Shell Programming and Scripting

awk - remove row if specific field is empty/blank

I have this text.filecharles darwin sam delight george washington johnson culper darwin sam delight micheal jackson penny lite and would like to remove the row, if the first field is blank. so the result would be: result.filecharles darwin sam ... (4 Replies)
Discussion started by: charles33
4 Replies

6. Shell Programming and Scripting

Format the file by deleting empty field

I have the test data with 10 column separated by comma and each column has more than 1000000 rows. Can anyone help me to find empty field in all columns and delete that empty field alone and lift that column up by one row. Data with empty field: A74203XYZ,A21718XYZ,A72011XYZ,A41095XYZ,... (7 Replies)
Discussion started by: zooby
7 Replies

7. 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

8. 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

9. UNIX for Dummies Questions & Answers

Count of Field for Non-Empty

Hi Guys, I wanted to count the number of records for a particular field of a file. whose fields are separated by comma"," I fI use this command. cat "filename" cut -sd "," -f13 | wc -l This shows all the lines count including the blank values for the field number 13. I wanted to count... (2 Replies)
Discussion started by: Swapna173
2 Replies

10. Shell Programming and Scripting

Awk scrip to remove empty field

Hi I have a file which looks like this name: Sally group: Group4 name: Tim group: Group1 name: Dan group: Group2 name: Chris group: Group3 name: Peter group: name: Fred group: name: Mary group: Group2 Well I want to get rid of the... (4 Replies)
Discussion started by: bombcan
4 Replies
Login or Register to Ask a Question