awk: How to check if field is blank?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: How to check if field is blank?
# 1  
Old 06-09-2005
awk: How to check if field is blank?

In awk, I'd like to check if a field is blank.
And by blank I mean, the field could be "" or " "
In other words, the field could either be empty, or be filled with spaces.

Would the regex look like this?

$5 ~ // { Action }?

What other ways are there?

Hmm.. in any case I think I've got the syntax wrong:
(($1 ~ /TestVal/) && ($2 ~ //)) { NUM_TEST_VAL++ }

Since the CSV file has fixed-length fields... I guess I'd have to specify "contains 32 spaces" ?

Last edited by yongho; 06-09-2005 at 11:40 AM..
# 2  
Old 06-09-2005
it would be better if u post a part of ur file .

Cheers
Rahul
# 3  
Old 06-09-2005
Hmm

Each record (each line) contains 30 fields so I'll post the first two fields, that are relevant.

"test_val ","32 white spaces here"

That is $1, and $2.
I was trying to create a pattern that identifies that the first field contains the "test_val" and that the second field is empty/blank/full-of-whitespaces (anything that tells me it's empty). That second field, has 32 fixed-length whitespaces. It would otherwise normally be filled with a 7-digit number at the beginning.

Ah, the forum removed the 32 white spaces, but imagien that they are there.
# 4  
Old 06-09-2005
$2 ~ /^[ ]*$/ { print "second field is empty" }
# 5  
Old 06-09-2005
Quote:
$2 ~ /^[ ]*$/ { print "second field is empty" }
So that's "Zero or more occurences of a space and the field must begin and end with a space" ?

Hmm.. I must be doing something wrong -- I'm gonna go back and try a few more things..
# 6  
Old 06-09-2005
Quote:
Originally Posted by yongho
So that's "Zero or more occurences of a space and the field must begin and end with a space" ?

Hmm.. I must be doing something wrong -- I'm gonna go back and try a few more things..
you have a CSV file with quoted fields
therefore.......
Code:
$2 ~ /^"[ ]*"$/ { print "second field is empty" }

# 7  
Old 06-09-2005
Ah..

I took yours and made a couple of adjustments..
I forgot that this specific field, even when blank, still contains quotation marks at both ends, the first char and last char.. I adjusted the regex in this way.

$2 ~ /^["][ ]+["]$/

1. First and last char must be a quotation.
2. One or more spaces much match in between.

This tells me if a field is blank/has no value (assuming there will always be quotes at either end).

Ah, I posted after you did, your regex looks better than mine.
Thanks. I like the * idea better since, perhaps, there may come a time when there will be no spaces in between the quotes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

2. Shell Programming and Scripting

Find a blank field

Find a blank field Hi I have set of fields that have some blank values, how to find that and get its line noumbers in output file. Ex: Col1 col2 col3 11 ss 103 12 104 13 105 14 se 106 (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

3. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

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

5. Shell Programming and Scripting

Help with removal of blank spaces from the second field!

Hi everyone.. I'm trying to eliminate multiple whitespaces from a file.. I must make use of shell script to eliminate whitespaces.. Take a look at the sample file 1 int main() 2 { 3 int a,b; 4 printf("Enter the values of a and b"); 5 scanf("%d%d",&a,&b); 6 if(a>b) ... (6 Replies)
Discussion started by: abk07
6 Replies

6. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

7. Shell Programming and Scripting

perl or awk, field length check

Hi Everyone, 1.txt a;1234;134;1111111 b;123;123;1111111 c;123;1334;1111111 d;1234;1234;1111111 output a;1234;134;1111111 c;123;1334;1111111 d;1234;1234;1111111 if field2 legth>3 or field3 length >3, then output. Please advice. Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

8. Shell Programming and Scripting

how to validate a field when it is blank using awk prog

Hi, I tried the below piece of code for my script to check whether it has a blank space for a particular field. if(f10==/]/){ print "Field 10 is Correct";} else{ print "Field 10 is Wrong"; } Please help me to know whether the "if" condition applied here is correct or do i... (14 Replies)
Discussion started by: meva
14 Replies

9. Shell Programming and Scripting

fill a NIL into the blank field

Hello, I have a record which split with "," I would like to check..if the field is empty and it will field "NIL" into the field. input 45111,40404,peter,,0303403,0,030304,john,,9,0, output 45111,40404,peter,NIL,0303403,0,030304,john,NIL,9,0, (8 Replies)
Discussion started by: happyv
8 Replies

10. Shell Programming and Scripting

how to include field separator if there are blank fields?

Hi, I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)
Discussion started by: ReV
19 Replies
Login or Register to Ask a Question