munna
munna

Reputation: 113

AWK scripting to to find highest number

1. \#id;clientid;product name;qty;price
1. 1;fooclient;product A;3;100
2. 2;booclient;product B;4;200
1. 3;xyzzycompany;product C;2;35000
1. 4;testclient;product B;1;190
1. 5;fooclient;product A;10;100
1. 6;testclient;product B;1;25000
1. 7;Mouccccccc;product C;2;300
1. 8;Deeccccccc;product C;2;10
1. 9;ICICT;product Z;12;45000
1. 10;AXISX;product D;14;75000
1. 11;Fcebook;product Z;12;65000

Need help to find highest price having clientid name using awk.

Options tried: filename: invoices_input.txt [having all above mentioned values]

awk 'BEGIN { FS = ";" } !/^#/ {print $2 " " $NF}' invoices_input.txt

Result:

fooclient 100
booclient 200
xyzzycompany 35000
testclient 190
fooclient 100
testclient 25000
Mouccccccc 300
Deeccccccc 10
ICICT 45000
AXISX 75000
Fcebook 65000

I am expecting AXISX to printed as highest numbered client.

Upvotes: 0

Views: 88

Answers (2)

oguz ismail
oguz ismail

Reputation: 50750

Try:

awk -F\; 'NR > 1 {
  if ($5 > price) {
    price = $5
    company = $2
  }
  else if ($5 == price) {
    company = company "\n" $2
  }
}
END { 
  print company
}' file

Upvotes: 0

karakfa
karakfa

Reputation: 67467

with awk and friends

$ sort -t';' -k2,2 -k5,5nr file              | 
  awk -F';' '!a[$2]++{print $1 "\t" $2,$NF}' | 
  sort -n                                    | 
  cut -f2-

clientid price
fooclient 100
AXISX 75000
Fcebook 65000
booclient 200
xyzzycompany 35000
testclient 25000
Mouccccccc 300
Deeccccccc 10
ICICT 45000

of course you can do all in awk as well.

without maintaining the order (assumes prices>0)

$ awk -F';' 'a[$2]<$NF {a[$2]=$NF} 
             END       {for(k in a) print k,a[k]}' file

clientid price
fooclient 100
ICICT 45000
Deeccccccc 10
xyzzycompany 35000
Fcebook 65000
testclient 25000
booclient 200
Mouccccccc 300
AXISX 75000

If you just need the client with highest price you don't need all this complexity

$ sort -t';' -k5,5nr file | sed 1q | cut -d';' -f2
AXISX

Upvotes: 2

Related Questions