Find max value problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find max value problem
# 1  
Old 06-17-2012
Find max value problem

I have a file about cpu load like this (the first column is time, the second column is cpu load):

11:34:01 0.10
11:34:37 0.05
11:35:13 0.03
11:35:59 0.06
11:38:00 0.02
11:38:09 0.17
11:39:48 0.06

I need to find the max value of the second column so I write the script as follows
Code:
max=0
cat a.txt | while read line
do
value=$(echo $line | awk '{print $2}')
if [ $(echo "$max < $value" | bc) -eq 1 ]; then
max=$value
fi
done
echo $max

this one bring the wrong max value. But when I edit with
Code:
while read line
do
..
done < a.txt

it brings the correct value. So why these bring the different answers. Please explain to me.
# 2  
Old 06-17-2012
In the first case the while loop is executed in a subshell ( except in Kornshell ) because of the pipe. This means that any variables set in the loop lose their value once the loop finishes and are no longer available outside the loop..
# 3  
Old 06-17-2012
I get it. Thank you so much.
# 4  
Old 06-17-2012
In bash 4.2 or later, you can do
Code:
shopt -s lastpipe

so that the last pipe runs in the current shell.

Code:
max=$(awk '{if($2>m)m=$2}END{print m}' a.txt)

is simpler and more efficient.

Last edited by binlib; 06-17-2012 at 10:55 AM.. Reason: Added an alternative solution
# 5  
Old 06-18-2012
I've forgotten about the powerful awk. thank you for your solution.
# 6  
Old 06-18-2012
just sort by second column, and retrieve the first value by head -1
Code:
 
$ sort -nr -k2 input.txt
11:38:09 0.17
11:34:01 0.10
11:39:48 0.06
11:35:59 0.06
11:34:37 0.05
11:35:13 0.03
11:38:00 0.02

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find Max value of line and print

I need to find the max value of all columns except the 1st column and print the answer along with the 1st column. Input 123xyz 0 0 1 2 0 0 0 0 0 0 0 234xyz 0 0 0 0 0 0 0 0 0 0 0 345xyz 0 0 1 0 0 0 ... (8 Replies)
Discussion started by: ncwxpanther
8 Replies

2. Shell Programming and Scripting

awk script to find min and max value

I need to find the max/min of columns 1 and 2 of a 2 column file what contains the special character ">". I know that this will find the max value of column 1. awk 'BEGIN {max = 0} {if ($1>max) max=$1} END {print max}' input.file But what if I needed to ignore special characters in the... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

3. Shell Programming and Scripting

Find max length of the field and then replace zero

hai guys, pick the 1st field and calculate max length. if suppose max length is 2, then compare the all records if <2 then add zero's prefix of the record. for ex: s.no,sname 1,djud 37,jtuhe in this max length of the 1st field is 2 right the output wil be s.no,sname 01,djud... (6 Replies)
Discussion started by: Suneelbabu.etl
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

Find the numeric value in a string and then check the max. value

hi, i have a string " 00000069 ThreadMonitor W WSVR0606W: Thread "WebContainer : 43|null" (00000069) was previously rep orted to be hung but has completed. It was active for approximately 47533430 milliseconds. There is/are 43 thread(s) in tot al in the server that still may be hung." ... (4 Replies)
Discussion started by: lovelysethii
4 Replies

6. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

7. Shell Programming and Scripting

How to find max value in a tabular data?

FILE_NAME VER TYPE DELETED_DATE CREATED_DATE SIZE LOCATION hello 1 d 2010-08-09 2010-08-09 4096 /home/xxxxx/project/ hello 2 d 2010-08-09 2010-08-09 4096 /home/xxxxx/project/ hello 3 d 2010-08-09 2010-08-09 4096 /home/xxxxx/project/ hello 4 d 2010-08-09 2010-08-09 4096 /home/xxxxx/project/ ... (4 Replies)
Discussion started by: classic
4 Replies

8. Shell Programming and Scripting

Find max number in a set

Is there a simple way of calculating the max number in a set of variables, so a=1 b=3 c=6 d=30 something that says e=max($a, $b, $c, $d) I've found a way to do it using: a="1" b="3" c="5" d="50" if ; then t=$a else (3 Replies)
Discussion started by: unclecameron
3 Replies

9. Shell Programming and Scripting

find max number

Hi, i have a file with numbers in it and i was wondering if there's a script i could use to find the max number and have that printed to a new file? example a.txt 18 26 47 34 27 so find the max number in a.txt and print it to b.txt. Thanks! (17 Replies)
Discussion started by: moonbaby
17 Replies

10. Shell Programming and Scripting

how to find min, max dates in a file

hello friends...:-) i need some help i have a file cantain like this Star1 ,NetWork,09/02/2008 Star1 ,NetWork,10/02/2008 Star1 ,NetWork,11/02/2008 Star2 ,NetWork,08/03/2008 Star2 ,NetWork,09/04/2008 Star2 ,NetWork,10/05/2008 i need to find out min, max dates the output look like... (6 Replies)
Discussion started by: gemini106
6 Replies
Login or Register to Ask a Question