The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 09:42 AM
how to check weather file is blank or not? jaydeep_sadaria Shell Programming and Scripting 5 12-15-2007 08:35 AM
fill a NIL into the blank field happyv Shell Programming and Scripting 8 03-23-2007 04:49 AM
how to include field separator if there are blank fields? ReV Shell Programming and Scripting 19 07-13-2005 05:50 AM
check for the first character to be blank anthreedhr UNIX for Dummies Questions & Answers 7 10-22-2003 11:05 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-09-2005
yongho yongho is offline
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 11:40 AM..
  #2 (permalink)  
Old 06-09-2005
rahul123_libra rahul123_libra is offline
Registered User
  
 

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

Cheers
Rahul
  #3 (permalink)  
Old 06-09-2005
yongho yongho is offline
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.
  #4 (permalink)  
Old 06-09-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
$2 ~ /^[ ]*$/ { print "second field is empty" }
  #5 (permalink)  
Old 06-09-2005
yongho yongho is offline
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..
  #6 (permalink)  
Old 06-09-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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 (permalink)  
Old 06-09-2005
yongho yongho is offline
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0