Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK Script: Testing for Null values 1

Status
Not open for further replies.

cgordon

Technical User
Oct 8, 2001
3
US
Hi,

I have an AWK script that worked until the data format changed. The 14th & 15th spaces are named RSV. This used to be only one space (until recently). Based on the value in RSV, I will print to a file in using PrintA or PrintB (different formats).

1545016908374 AVL111111
1545011234567M AVL111111

I was using

if (rsv!=" ")
print..... (Use PrintA)
else
print..... (Use PrintB)

When the 15th position did not exist, this worked. Now RSV is two characters/digits. How do I test for a null value??

Thank you
 
What happens if you simply add an extra blank to your test?

if (rsv!=" ")
print..... (Use PrintA)
else
print..... (Use PrintB)

If this doesn't solve your problem, post again to tell us exactly what is in rsv when you want to use printa and when you want to use printb. Also, maybe you need to tell us exactly how rsv is set from the input line.
CaKiwi
 
This is part of an AWK script called from a batch file.

grep AVL filename | awk -f ntnirpt.a

In ntnirpt.a, the first few lines describe the format.

rsv = substr($0,14,2)
.. then a series of counters, based on the RSV indicator. A = A+1, B = B+1, ...

Then a print statement.

I tried to just add an extra space, but then it treated every entry as if there was data there.
1234( ) 2222(M ) 3333( ) 4444(N )

Output should be (sample):
1234 2222(M ) 3333 4444(N )

The desired result is where the lack of a RSV means that no () are included and any indicator would be included in the (). The resulting file is then read into Excel, where more macros look for consecutive numbers without ().


My printf statements are:

if(rsv!=" ")
printf(",%s(%s)",stn,rsv) > "tni"mkt".csv"
else
printf(",\"=text(%s,\"\"0000\"\")\"",stn) > "tni"mkt".csv"

mkt is defined as the first three characters of the dataline.
stn is defined as the 10-13 digits of the dataline.

Thank you
:)





 
I don't understand why the test rsv!=" " isn't working. How about trying

if (substr(rsv,1,1)!=" " || substr(rsv,2,1)!=" ")

if this doesn't work, add a debug print statement before the test e.g.

print "- " length(rsv) " - " substr(rsv,1,1) " - " substr(rsv,2,1) " -"

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top