Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Extract minimum/maximum using awk

Shell Programming and Scripting


Tags
awk, highest value, lowest value

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-10-2013
gwgreen1's Unix or Linux Image
gwgreen1 gwgreen1 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 10 July 2013, 6:27 AM EDT
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Facebook Extract minimum/maximum using awk

From the below table I want to print highest value and lowest value using awk script.

Code:
aaa 55 66 96 77
ggg 22 96 77 23
ddd 74 58 18 3
kkk  45 89 47 92
zzz  34 58 89 92

Thanks, Green

Moderator's Comments:
Extract minimum/maximum using awk edit by bakunin: it sure is not news to you that you should use CODE-tags, no? And that we do not want such informative thread titles as "help needed" you sur haven't heard for the first time either, did you?

I changed the thread title and inserted the missing CODE-tags, but you should really consider changing your attitude and posting behavior.

Last edited by bakunin; 07-10-2013 at 07:38 AM..
Sponsored Links
    #2  
Old Unix and Linux 07-10-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 3 December 2016, 6:33 AM EST
Location: San Jose, CA, USA
Posts: 9,722
Thanks: 459
Thanked 3,320 Times in 2,840 Posts
Try:

Code:
awk '
first == "" {
	m = $2
	M = $2
	first = "done"
}
{	for(i = 2; i <= NF; i++) {
		if($i < m) m = $i
		if($i > M) M = $i
	}
}
END {	print m, M
}' table

If you are using a Solaris system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /usr/bin/awk or /bin/awk.

If table contains your sample input, the output produced is:

Code:
3 96

which are the minimum and maximum values in your data (ignoring the 1st column in each line).
The Following User Says Thank You to Don Cragun For This Useful Post:
RavinderSingh13 (07-10-2013)
Sponsored Links
    #3  
Old Unix and Linux 07-10-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 3 December 2016, 4:56 AM EST
Location: Amsterdam
Posts: 11,096
Thanks: 440
Thanked 3,105 Times in 2,747 Posts
Just for fun:

Code:
awk 'NR==1 || $1==""{next} l=="" || $1<l{l=$1} $1>h{h=$1} END{print l,h}' RS=" " file

    #4  
Old Unix and Linux 07-10-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts
Another approach using GNU awk asort function:

Code:
xargs -n1 < file | gawk '/^[0-9]*$/{A[NR]=$1}END{n=asort(A);print A[1],A[n]}'

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting average ,maximum and minimum value zozoo Shell Programming and Scripting 3 02-04-2013 10:30 PM
Finding minimum maximum and average aroragaurav.84 Shell Programming and Scripting 5 12-13-2012 04:50 PM
Print minimum and maximum values using awk quincyjones Shell Programming and Scripting 2 12-13-2012 07:39 AM
minimum and maximum from columns jacobs.smith Shell Programming and Scripting 4 09-11-2012 09:33 PM
for each different entry in column 1 extract maximum values from column 2 in unix/awk Diya123 Shell Programming and Scripting 1 07-19-2011 03:37 PM



All times are GMT -4. The time now is 01:42 PM.