Operating System - Linux
1827279 Members
2627 Online
109717 Solutions
New Discussion

"if" processing in "awk".

 
SOLVED
Go to solution
Stuart Abramson
Trusted Contributor

"if" processing in "awk".

I have an "awk" program, in which I can't get the "if" conditions to work:

syminq -sym -v | awk '
/Device Phys/ {DEV=substr($5,11); next}
/Product ID/ {PROD=substr($4,1,4); next}
/Capacity/ {CAP=$5; next}
/12-digit Symm/ {ARRAY=substr($5,9); next}
/Device Symm/ {LUN=$5; next}
/ Device Ty/ {DEVTYP=substr($4,1,3); next} # BCV or REG
/Volume Logix/ {VCM=$5; next}
/Meta/ {META=$4; next}
/VxVM/ {CLRID=substr(ARRAY,3)
TYP=REG
if (META != "N/A") TYP=META
if (DEVTYP = "BCV") TYP=BCV
if (VCM = "YES") TYP="VCM"
print CLRID LUN "000", ARRAY, TYP, DEV, CAP}' \
| sort


I just always get "TYP is equal to "VCM", which it's not. Can someone tell me what I'm doing wrong?
6 REPLIES 6
Greg Vaidman
Respected Contributor
Solution

Re: "if" processing in "awk".

In your if statements, you want to use "==" and not "=". What you're doing is actually assigning the value rather than testing it, and the return code of an assignment is usually true, so that's why it's working that way.

--Greg
Rodney Hills
Honored Contributor

Re: "if" processing in "awk".

Don't use "=" to compare, use "==". A single "=" is an assignment which will return true in all your if statements.

HTH

-- Rod Hills
There be dragons...
Pat Lieberg
Valued Contributor

Re: "if" processing in "awk".

Your syntax looks ok to me. It looks like you are testing three different variables to set TYP. The way its written, each of those if statements will be evaluated in sequence and every time one of them evaluates as true, the value of TYP is overwritten. The VCM statement being last might be why you always get that as the result.

Did you mean to use an "else if" statement? You would use that if one of the types is the default and you only want to deviate if certain conditions are true. For example, the code below sets TYP to META unless the if or the else if statement evaluate to true.

if (DEVTYP = "BCV") TYP=BCV
else if (VCM = "YES") TYP="VCM"
else (META != "N/A") TYP=META


Pat Lieberg
Valued Contributor

Re: "if" processing in "awk".

Ha! That's what I get for not looking close enough and for typing slow. Your syntax was wrong. Greg and Rodney both are right, you need to use ==.
Hein van den Heuvel
Honored Contributor

Re: "if" processing in "awk".


Those == for testing, and a bunch more quotes are needed:

TYP="REG";
if (META != "N/A") TYP="META"
if (DEVTYP == "BCV") TYP="BCV"
if (VCM == "YES") TYP="VCM"

Hein.
Stuart Abramson
Trusted Contributor

Re: "if" processing in "awk".

Thanks all. "=="...