![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
it would be better if u post a part of ur file .
Cheers Rahul |
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
$2 ~ /^[ ]*$/ { print "second field is empty" }
|
|
#5
|
|||
|
|||
|
Quote:
Hmm.. I must be doing something wrong -- I'm gonna go back and try a few more things.. |
|
#6
|
||||
|
||||
|
Quote:
therefore....... Code:
$2 ~ /^"[ ]*"$/ { print "second field is empty" }
|
|
#7
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |