guyconfusedwithawk
guyconfusedwithawk

Reputation: 23

Awk not printing what is wanted

my attempt:

awk '$4 != "AZ" && max<$6 || NR==1{ max=$6; data=$0 } END{ print data }' USA.txt

I am trying to print the row that does NOT have "AZ" in the 4th column and the greatest value in the 6th column

the file has 6 colums firstname lastname town/city state-abv. zipcode score

Shellstrop Eleanor Phoenix AZ 85023 -2920765
Shellstrop Donna Tarantula_Springs NV 89047 -5920765
Mendoza Jason Jacksonville FL 32205 -4123794
Mendoza Douglas Jacksonville FL 32209 -3193274 
Peleaz Steven Jacksonville FL 32203 -3123794 

Upvotes: 1

Views: 63

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Based on your attempts, please try following awk code. This checks if 4th field is NOT AZ then it compares previous value of max with current value of $6 if its greater than previous value then it assigns current $6 to max else keeps it to previous value. In END block of awk program its printing its value.

awk -v max="" '$4!="AZ"{max=(max>$6?max:$6)} END{print max}' Input_file

To print complete row for a maximum value found would be:

awk -v max="" '$4!="AZ"{max=(max>$6?max:$6);arr[$6]=$0} END{print arr[max]}' Input_file

Upvotes: 3

Related Questions