Need to find the greates value using AWK.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to find the greates value using AWK.
# 1  
Old 11-18-2009
Need to find the greates value using AWK.

Friends, I have to find out the greatest amoings three values using awk. Actually i have to run the iostat -d command on my linux box for 5 or 6 iterations and print the largest value for tps at each iteration
for e.g

Code:
  iostat -d 3 3
Linux 2.6.18-8.el5 (abhijit)    11/19/2009      _i686_  (2 CPU)

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda              10.22       541.96       181.35     772064     258352
sdb               0.16         2.41         0.00       3431          0
fd0               0.00         0.01         0.00         16          0

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               1.00         0.00        58.67          0        176
sdb               0.00         0.00         0.00          0          0
fd0               0.00         0.00         0.00          0          0

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               3.67         2.67       173.33          8        520
sdb               0.00         0.00         0.00          0          0
fd0               0.00         0.00         0.00          0          0

Now using awk i have to print the biggest value of tps in each iteration. Can some one pls guide me as to how to acieve this.?
# 2  
Old 11-18-2009
Code:
awk 'BEGIN{n=0}
{
if($0 ~ /Device/){
  flag=1
  if(n!=0){
    print n
    n=0
  }
}
else{
  if($2>n)
    n=$2
}
}
END{
  if(n>0)
    print n
}' a.txt

# 3  
Old 11-19-2009
A different approach Smilie
Code:
awk 'END{print _}{if(NF){_=(/tps/)?0:(($2>=_)?$2:_)}else{print _}}'

# 4  
Old 11-20-2009
worked with the following syntax
Code:
awk '{if(NF){_=(/tps/)?0:(($2>=_)?$2:_)}else{print _}}'

can anyone be pls kind enough to explain to me the meaning of the code.

Last edited by achak01; 11-20-2009 at 01:37 AM.. Reason: spelling mistakes
# 5  
Old 11-20-2009
Quote:
Originally Posted by achak01
Code:
awk '{if(NF){_=(/tps/)?0:(($2>=_)?$2:_)}else{print _}}'

can anyone be pls kind enough to explain to me the meaning of the code.
Just a very compact way to represent nested IF statements Smilie
The extended version of my answer should look like:

Code:
cat FILE |\
awk '{
      if(NF){
             if(/tps/){
                       var=0
                      }
             else {
                   if($2>=var){
                               var=$2
                               }
                   }
            }
      else{
           print var
           }
}
END{
    print var
   }'

# 6  
Old 11-21-2009
I guess achak01 want to get the greatest valus for sda, sdb, and fd0 each.

so the output should be:

Code:
sda 10.22
sdb 0.16
fd0 0.00

So need confirm first.
# 7  
Old 11-23-2009
The code snippet which i had pasted worked out in Linux. But for HP-UX. i am at my wits end. when u run the command sar -d $interval $iteration it gives out values like pasted below
Code:
sar -d 3 3

HP-UX unknown B.11.23 U 9000/800    11/23/09

05:11:30   device   %busy   avque   r+w/s  blks/s  avwait  avserv
05:11:33   c2t0d0    0.66    0.50       2      27    0.00    9.99
05:11:36   c2t0d0    0.33    0.50       1      11    0.00    5.18
05:11:39   c2t0d0    0.33    0.50       0       5    0.00    6.66

. Now at each iteration in want the greatest value printed for %busy column. here it has 1 disk hence its showing 1 value only. what if there are multiple disks. then there will be surely two values for each iteration. that time only the largest is to eb printed. Pls let me know how to proceed on this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace in awk

I have a file that I am trying to find a specific word, then replace text within that string. file TestA2015 TestB2016 Example. Replace TestB2016 to TestB0000, so if TestB is found replace the original "2016" to "0000". Thank you :). awk tried awk '{ sub(/TestB$/, "0000", $6) }1'... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk find closest

Hi All I am sorry but I have to open a new thread about my issue. My input files are: file_1 ID_1 10 ID_2 15 ID_3 32 ID_4 45 ID_5 66 ID_6 79 ID_7 88 file_2 ID_3 ID_5My output file should be ID_3 ID_1(-22) ID_2(-17) ID_4(-13) ID_5(34) (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

3. Shell Programming and Scripting

find percentage - awk

Please help me with this ... Input file /vol/test1 10G /vol/test2 1G /vol/test3 200G /vol/test4 3G Output File /vol/test1 10G - - 9G - /vol/test2 1024M - - 921M - /vol/test3 200G - - 180G - /vol/test4 3072M - - 2764M - Basically if Column 2 ( which is... (6 Replies)
Discussion started by: greycells
6 Replies

4. Shell Programming and Scripting

awk to find the max

Experts, Here is my question. I have got file like below # cat file XYZb,24,26,6 XYZc,24,26,6 XYZe,24,25,5 XYZf,23,29,5 XYZi,16,25,5 XYZj,24,26,7 XYZn,17,23,4 XYZz,23,29,5 Now, I want to print the line's Column1 whose Column 3 is the max of all. My code is awk -F',' 'BEGIN {max... (9 Replies)
Discussion started by: sathyaonnuix
9 Replies

5. Shell Programming and Scripting

awk conditional find

Hi, I have a file in the following format: aabbba 25.31806899 baaabb 38.21808852 cccccu 1.31819523 552258121.31818253 ffddybb 5.41815555 almcamc87561812689 223aqas5.661828345 adacaaaaaaa1821285 adacaaaaaaa1821286 smckaa 3.81828756 ada2512510c1821287 ada2522511c1821328... (4 Replies)
Discussion started by: alex2005
4 Replies

6. Shell Programming and Scripting

Using awk to find sentences.

I am trying to print out sentences that meets a regular expression in awk (I’m open to using other tools, too). I got the regular expression I want to use, "(\+ \{4\})" from user ripat in a grep forum. Unfortunately with grep I couldn't print only the sentence. While searching for awk... (8 Replies)
Discussion started by: danbroz
8 Replies

7. Shell Programming and Scripting

Awk find and sel

Hi to all! I need to make an script to find when an user changes the IP. The log file have this simple structure; example.txt Jack 192.168.1.2 Tom 192.168.12.225 Mary 192.168.1.22 Jack 192.168.1.5 Patrick 192.168.1.88If match the same user in the first column and have differents IP in... (3 Replies)
Discussion started by: LordXeno
3 Replies

8. Shell Programming and Scripting

Using awk to find next word available

I'm trying to use awk to extract certain text from a file. The file looks like this: Start 1 10 2 20 3 30 ... ... ... <some word> Now what I want to extract is the list of numbers (without the search text printed) that occur between "Start" and <some word>. The catch is... (23 Replies)
Discussion started by: NickC
23 Replies

9. Shell Programming and Scripting

Find avg using awk

Hi, i need some help plz... The file data.txt contains: code of student,surname and name,code of lesson,grade of lesson.The number of lessons of each student is not the same. 25,Jackson Steve,12,4,34,2,65,2 29,Jordan Mary,13,6,23,8,56,4,34,2 04,Leven Kate,14,6,15,6,26,4 34,Owen... (10 Replies)
Discussion started by: Steve_09
10 Replies

10. UNIX for Dummies Questions & Answers

grep, find or awk?

Since I found this very helpful forum and friendly people, I have a small request. I use AIX Unix and would like to know if there's a simple way how to do a kind of compare between two files. Should I use some grep, find or awk? I have 2 text files, let's name them file1 and file2. What I... (4 Replies)
Discussion started by: netrom
4 Replies
Login or Register to Ask a Question