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!

Need awk script to find max and min of certain type 1

Status
Not open for further replies.

123naz

Technical User
Nov 17, 2002
4
SA
Hi All,
I have an input file in the following format:

Input File:
5 apple
10 mango
10 orange
7 cherry
15 apple
8 cherry
9 mango
12 orange
10 apple
101 mango
40 cherry
11 orange


And I would like to get the output as below:

Output File:

Output File:
Name Max Min
apple 15 5
mango 101 9
orange 12 10
cherry 40 7


Find the maximum and minimum of each fruit category with an awk script. Could you please help me to sort out this...


Thanks in advance

Naz
 
Hi

Code:
awk '[teal]!([/teal][navy]$2in[/navy] m[teal])||[/teal]m[teal][[/teal][navy]$2[/navy][teal]]>[/teal][navy]$1[/navy][teal]{[/teal]m[teal][[/teal][navy]$2[/navy][teal]]=[/teal][navy]$1[/navy][teal]}[/teal]M[teal][[/teal][navy]$2[/navy][teal]]<[/teal][navy]$1[/navy][teal]{[/teal]M[teal][[/teal][navy]$2[/navy][teal]]=[/teal][navy]$1[/navy][teal]}[/teal]END[teal]{[/teal][COLOR=chocolate]printf[/color][green][i]"%-10s %10s %10s[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal],[/teal][green][i]"Name"[/i][/green][teal],[/teal][green][i]"Max"[/i][/green][teal],[/teal][green][i]"Min"[/i][/green][teal];[/teal][b]for[/b][teal]([/teal]i [teal]in[/teal] m[teal])[/teal][COLOR=chocolate]printf[/color][green][i]"%-10s %10s %10s[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green][teal],[/teal]i[teal],[/teal]M[teal][[/teal]i[teal]],[/teal]m[teal][[/teal]i[teal]][/teal][teal]}[/teal]
' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Next time will wait until you post the evidence of your own efforts.


Feherke.
 
Thanks a lot Feherke for your reply
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top