The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[bash] Check if variable is set or blank noratx Shell Programming and Scripting 1 03-28-2008 06:42 AM
how to check weather file is blank or not? jaydeep_sadaria Shell Programming and Scripting 5 12-15-2007 05:35 AM
fill a NIL into the blank field happyv Shell Programming and Scripting 8 03-23-2007 01:49 AM
how to include field separator if there are blank fields? ReV Shell Programming and Scripting 19 07-13-2005 01:50 AM
check for the first character to be blank anthreedhr UNIX for Dummies Questions & Answers 7 10-22-2003 07:05 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-09-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 07:40 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 06-09-2005
Registered User
 

Join Date: May 2005
Posts: 46
it would be better if u post a part of ur file .

Cheers
Rahul
Reply With Quote
  #3  
Old 06-09-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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.
Reply With Quote
  #4  
Old 06-09-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
$2 ~ /^[ ]*$/ { print "second field is empty" }
Reply With Quote
  #5  
Old 06-09-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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..
Reply With Quote
  #6  
Old 06-09-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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" }
Reply With Quote
  #7  
Old 06-09-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0