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" ?

it would be better if u post a part of ur file .

Cheers
Rahul

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.

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


  1. ↩︎

you have a CSV file with quoted fields
therefore.......

$2 ~ /^"[ ]*"$/ { print "second field is empty" }

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.

great minds think alike.....