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!

Nawk multiple conditions 3

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Hi All,

I'm trying to write a ksh script that uses a nawk statement embedded between two for loops.
My issue is with getting nawk to recognise the second condition, in this case the $state.
It works fine for just the $robot and does what I expect it to.
i.e
nawk 'BEGIN {OFS=" "} $4=='"$robot"' {print $5,$1,$9}' /tmp/media | sort -n

OUTPUT:
1 UPX069 AVAILABLE
2 UPX128 AVAILABLE
3 UPX071 AVAILABLE
4 UPX113 AVAILABLE
5 UPX072 AVAILABLE
e.t.c

However, when I try and add to it with the second for loop and the $state my nawk output is blank.

CODE:
#!/bin/ksh

for robot in 0 1 ; do
((server=$robot+1))
for state in AVAILABLE FULL ACTIVE FROZEN; do
echo $state
printf "%-8s" "Slot:";printf "%-8s\n" "ID:"
nawk 'BEGIN {OFS=" "} $4=='"$robot"' && $9=='"$state"' {print $5,$1,$9}' /tmp/media | sort -n
done


done

I've tried doing it this way as well;

nawk 'BEGIN {OFS=" "} {if ($4=='"$robot"' && $9=='"$state"')}{print $5,$1,$9}' /tmp/media | sort -n

But this seems to ignore both the $4 and $9 matches.
I guess this is probably terrible code anyway, but it's xmas and I'm trying to pass the day by learning something useful. ;-)

Appreciate any help as always.
Alan
 
nawk 'BEGIN {OFS=" "} $4=='"$robot"' && $9==[!]"[/!]'"$state"'[!]"[/!] {print $5,$1,$9}' /tmp/media | sort -n

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Fantastic, quick and correct as usual.

Thanks very much PHV.

Much obliged.
 
Personally I try and avoid all that horrible punctuation by assigning shell variables to awk variables:

Code:
nawk -v robot=$robot -v state=$state '
    BEGIN {OFS="      "}
    $4==robot && $9==state {print $5,$1,$9}
' /tmp/media | sort -n

Annihilannic.
 
Cheers Anni, that looks better.
Not sure who gave you the star but have another from me.

Thanks for your guidance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top