Need to pick max values of the columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to pick max values of the columns
# 1  
Old 06-27-2014
Need to pick max values of the columns

Hi,

I have sar disk reports like below sample:

Code:
01:01:00    hdisk24      0      0.0        0        0      0.0      0.0
            hdisk15      0      0.0        0        3      0.0      5.5
            hdisk20      0      0.0        2        1      0.0      1.9
            hdisk19      1      0.0        2        1      0.0      4.5
            hdisk18      0      0.0        0        0      0.0      0.0
            hdisk16      0      0.0        0        0      0.0      0.0
            hdisk22      0      0.0        0        0      0.0      0.0
            hdisk14      0      0.0        1        5      0.1      1.0
            hdisk21      1      0.0        2        1      0.0      3.8
            hdisk17      0      0.0        0        0      0.0      0.5
            hdisk23      0      0.0        0        0      0.0      0.0
             hdisk2      0      0.0        0        0      0.0      0.0
             hdisk0      0      0.0        0        0      0.0      1.8


01:06:00    hdisk24      0      0.0        0       24      0.0      7.3
            hdisk15      0      0.0        1       95      0.1      4.2
            hdisk20      0      0.0        2        1      0.0      2.3
            hdisk19      0      0.0        2        1      0.0      2.2
            hdisk18      0      0.0        0        0      0.0      0.0
            hdisk16      0      0.0        0        0      0.0      0.0
            hdisk22      0      0.0        0        0      0.0      0.0
            hdisk14      0      0.0        1        9      0.9      2.0
            hdisk21      0      0.0        2        1      0.0      2.8
            hdisk17      0      0.0        0        0      0.0      1.1
            hdisk23      0      0.0        0       22      0.0      9.1
             hdisk2      0      0.0        0        0      0.0      0.0
             hdisk0      0      0.0        0        0      0.0      2.2


01:11:00    hdisk24      0      0.0        0        0      0.0      0.0
            hdisk15      0      0.0        0        6      0.0      2.5
            hdisk20      0      0.0        2        1      0.0      1.4
            hdisk19      0      0.0        2        1      0.0      1.5
            hdisk18      0      0.0        0        0      0.0      0.0
            hdisk16      0      0.0        0        0      0.0      0.0
            hdisk22      0      0.0        0        0      0.0      0.0
            hdisk14      0      0.0        1       10      0.3      1.6
            hdisk21      0      0.0        2        1      0.0      2.2
            hdisk17      0      0.0        0        0      0.0      0.7
            hdisk23      0      0.0        0        0      0.0      0.0
             hdisk2      0      0.0        0        0      0.0      0.0
             hdisk0      0      0.0        0        0      0.0      1.6

I need to pick the max values of each column and discard the hard drive (column 2) so that output would be like:
Code:
01:01:00    1      0.0        2        5      0.1      5.5
01:06:00    0      0.0        2       95      0.9      9.1	
01:11:00    0      0.0        2       10      0.3      2.5

Pls advise, thank you.
# 2  
Old 06-27-2014
Other than not printing the tab character that you show at the end of the 2nd line of your specified sample output, the following seems to do what you want:
Code:
awk '
$1 ~ /:/ {
	if(t) p()
	t = $1
	mf = NF - 1
	for(i = 3; i <= NF; i++) 
		m[i - 1] = $i
	next
}
function p(	i) {
	printf("%s %4s ", t, m[2])
	for(i = 3; i <= mf; i++)
		printf("%8s%s", m[i], i == mf ? "\n" : " ")
}
{	for(i = 2; i <= mf; i++)
		if(m[i] < $i)
			m[i] = $i
}
END {	p()
}' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /sur/xpg6/bin/awk, or nawk.
# 3  
Old 06-27-2014
Bit messy but this should work:

Code:
awk '/^[0-9]/{ptime=time;time=$1;f++}
{
    if ((NF) && (f!=a)) {
         printf ptime "\t" max1 "\t" max2 "\t" max3 "\t" max4 "\t" max5 "\t" max6 "\n"
         max1=max2=max3=max4=max5=max6=""
         }

    if (NF){
         max1=(max1>$(NF-5)?max1:$(NF-5))
         max2=(max2>$(NF-4)?max2:$(NF-4))
         max3=(max3>$(NF-3)?max3:$(NF-3))
         max4=(max4>$(NF-2)?max4:$(NF-2))
         max5=(max5>$(NF-1)?max5:$(NF-1))
         max6=(max6>$NF?max6:$NF)
        }
}
{a=f}
END {printf time "\t" max1 "\t" max2 "\t" max3 "\t" max4 "\t" max5 "\t" max6 "\n"}
'

# 4  
Old 06-28-2014
Also try :

Code:
awk '  function p(  i){
		for(i=1; i<=k; i++)
		printf("%8s%s", i == 1 ? sprintf("%4s%s%8s",f,OFS,a[i]) : a[i] , i==k ? RS : OFS)
		delete a
	}
        {
	   s  = 2  
        }
	$1~/:/{ 
		if(f)p()
		f = $1
		s = 3 
	}
	NF{ 
		k = 0
		for(i=s; i<=NF; i++)
		{
			++k
			a[k] = (k in a && a[k] > $i) ? a[k] : $i 
		}
	} 
	END{
		p()
	}
    ' file

Code:
01:01:00        1      0.0        2        5      0.1      5.5
01:06:00        0      0.0        2       95      0.9      9.1
01:11:00        0      0.0        2       10      0.3      2.5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 Query to pick hierarchy values

Dear Team I am using DB2 v9 . I have a condition to check roles based on hierarchies like below example. 1.Ramesh has Roles as "Manager" and "Interviewer" 2.KITS has Roles as "Interviewer" 3.ANAND has Roles as "Manager" and "Interviewer" select * FROM TESTING NAME ... (6 Replies)
Discussion started by: Perlbaby
6 Replies

2. Shell Programming and Scripting

Regex in Shell Scripting to pick values

Hi After lot of trial and error I am really bowled out with the requirement in hand and honestly you are my last hope Here is what I want to achieve Values *IF *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *GT 70.00 *AND *VALUE... (20 Replies)
Discussion started by: radioactive9
20 Replies

3. Shell Programming and Scripting

Add sum of columns and max as new row

Hi, I am a new bie i need some help with respect to shell onliner; I have data in following format Name FromDate UntilDate Active Changed Touched Test 28-03-2013 28-03-2013 1 0.6667 100 Test2 28-03-2013 03-04-2013 ... (1 Reply)
Discussion started by: gangaraju6
1 Replies

4. Shell Programming and Scripting

How to get min and max values using awk?

Hi, I need your kind help to get min and max values from file based on value in $5 . File1 SP12.3 stc 2240806 2240808 + ID1_N003 ID2_N003T0 SP12.3 sto 2241682 2241684 + ID1_N003 ID2_N003T0 SP12.3 XE 2239943 2240011 + ID1_N003 ID2_N003T0 SP12.3 XE 2240077 2241254 + ID1_N003 ... (12 Replies)
Discussion started by: redse171
12 Replies

5. UNIX for Dummies Questions & Answers

Pick out columns according to list of column names

Hi Everyone: I'm new to linux and I was wondering what the best way to approach the following problem was. I have 2 files: File A: ID123 ID234 ID456 File B: ID123 ID234 ID345 ID456 A B C D E F G H I J K L Based on the list of IDs in File A, I want to output only the... (3 Replies)
Discussion started by: chongm88
3 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

7. Shell Programming and Scripting

pick columns

I want to be able pick columns from 2 files with similar reference data: File 1 Last NameFirst NameSalaryCityDunnJohn $42,000.00 ChicagoGrantSuzy $95,000.00 GaryLoweMike $80,000.00 MilwaukeeGrantMike $59,000.00 JolietLoweKaren $48,000.00 South BendFile 2 TitleFirst NameLast... (2 Replies)
Discussion started by: sigh2010
2 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

9. Shell Programming and Scripting

Local max values of a file

I have a data file of two columns (corresponding to the x and y axes of a graph) separated by a comma. I want to find the local maximum values and print them to a file. It has been a while since I've done any scripting and I've forgotten everything. I have a few basic questions. 1.) How do... (1 Reply)
Discussion started by: jemm
1 Replies

10. Shell Programming and Scripting

max values amd min values

Hello every one, I have following data ***CAMPAIGN 1998 CONTRIBUTIONS*** --------------------------------------------------------------------------- NAME PHONE Jan | Feb | Mar | Total Donated ... (12 Replies)
Discussion started by: devmiral
12 Replies
Login or Register to Ask a Question