Print the line containing the maximum value in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Print the line containing the maximum value in a column
# 8  
Old 02-03-2006
Hi KingKong,

Now I dont have any idea why its not working for you and working fine for me. Can you run it with set -x enabled and paste the output here.

Regards,
Gaurav
# 9  
Old 02-03-2006
Hey Gaurav!

The output is with set -x enabled:

+ max
cat: soln.sh: No such file or directory
+ max=0
+ cat file.txt
+ read LINE
++ echo 1001 34.5 68.7 67
++ awk '{print $2}'
+ fir=34.5
++ echo 1001 34.5 68.7 67
++ awk '{print $3}'
+ sec=68.7
+ '[' 34.5 -gt 0 ']'
./max: line 9: [: 34.5: integer expression expected
+ '[' 68.7 -gt 0 ']'
./max: line 12: [: 68.7: integer expression expected
+ read LINE
++ echo 1002 22.0 40.1 32
++ awk '{print $2}'
+ fir=22.0
++ echo 1002 22.0 40.1 32
++ awk '{print $3}'
+ sec=40.1
+ '[' 22.0 -gt 0 ']'
./max: line 9: [: 22.0: integer expression expected
+ '[' 40.1 -gt 0 ']'
./max: line 12: [: 40.1: integer expression expected
+ read LINE
++ echo 1003 11.3 34.8 45
++ awk '{print $2}'
+ fir=11.3
++ echo 1003 11.3 34.8 45
++ awk '{print $3}'
+ sec=34.8
+ '[' 11.3 -gt 0 ']'
./max: line 9: [: 11.3: integer expression expected
+ '[' 34.8 -gt 0 ']'
./max: line 12: [: 34.8: integer expression expected
+ read LINE
+ grep 0 file.txt
1001 34.5 68.7 67
1002 22.0 40.1 32
1003 11.3 34.8 45

Regards
Kingkong
# 10  
Old 02-03-2006
Hi KingKong,

The reason is that the value of max variable is not getting changed. Even I don't know why.

Try with the following modified script and tell me what happened with this, even I am eager to know why this is happening.

Quote:
set -x
max=0
cat file.txt|while read LINE
do
fir=`echo $LINE|awk '{print $2}'`
sec=`echo $LINE|awk '{print $3}'`
if [ $fir -gt $max ]; then
typeset max=$fir
fi
if [ $sec -gt $max ];then
typeset max=$sec
fi
done

grep $max file.txt
I am sure that this will work,

Cheers,
Gaurav
# 11  
Old 02-03-2006
Hey Gaurav,

This time the output is as follows:

+ max
+ max=0
+ cat file.txt
+ read LINE
++ echo 1001 34.5 68.7 67
++ awk '{print $2}'
+ fir=34.5
++ echo 1001 34.5 68.7 67
++ awk '{print $3}'
+ sec=68.7
+ '[' 34.5 -gt 0 ']'
./max: line 8: [: 34.5: integer expression expected
+ '[' 68.7 -gt 0 ']'
./max: line 11: [: 68.7: integer expression expected
+ read LINE
++ echo 1002 22.0 40.1 32
++ awk '{print $2}'
+ fir=22.0
++ echo 1002 22.0 40.1 32
++ awk '{print $3}'
+ sec=40.1
+ '[' 22.0 -gt 0 ']'
./max: line 8: [: 22.0: integer expression expected
+ '[' 40.1 -gt 0 ']'
./max: line 11: [: 40.1: integer expression expected
+ read LINE
++ echo 1003 11.3 34.8 45
++ awk '{print $2}'
+ fir=11.3
++ echo 1003 11.3 34.8 45
++ awk '{print $3}'
+ sec=34.8
+ '[' 11.3 -gt 0 ']'
./max: line 8: [: 11.3: integer expression expected
+ '[' 34.8 -gt 0 ']'
./max: line 11: [: 34.8: integer expression expected
+ read LINE
+ grep 0 file.txt
1001 34.5 68.7 67
1002 22.0 40.1 32
1003 11.3 34.8 45

Regards,
Kingkong
# 12  
Old 02-03-2006
Hi Kingkong,

No idea now, lets wait for some guru to throw some light on it. Don't know why you are not able to set the value of max variable.
One quick question are you using the exact code I m pasting here or did you made some small change in it.
Hey all unix gurus help us out.

Gaurav
# 13  
Old 02-03-2006
Hi Gaurav,

I pasted the exact code into a script named max.

Kingkong
# 14  
Old 02-03-2006
Hi ,
May be this is the reason.
Change the name of your script.
And it will work out
max is the name of variable and some conflict may be there.
so change the name of script and run it

Gaurav
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

If the 1th column of file f1 and file f2 is the same, then export those line with maximum string of

please help to write a awk command-line programs to achieve the following functions: Thank in advance. Requeset Description: compare two files f1 and f2, export to file f3: 1 Delete duplicate rows of in file f1 and file f2 2 If the 1th column of file f1 and file f2 is the same, then export... (1 Reply)
Discussion started by: weichanghe2000
1 Replies

3. Shell Programming and Scripting

Print whole line with highest value from one column

Hi, I have a little issue right now. I have a file with 4 columns test0000002,10030010330,c_,218 test0000002,10030010330,d_,202 test0000002,10030010330,b_,193 test0000002,10030010020,c_,178 test0000002,10030010020,b_,170 test0000002,10030010330,a_,166 test0000002,10030010020,a_,151... (3 Replies)
Discussion started by: Ebk
3 Replies

4. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

5. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

8. Shell Programming and Scripting

read file line by line print column wise

I have a .csv file which is seperated with (;) inputfile --------- ZZZZ;AAAA;BBB;CCCC;DDD;EEE; YYYY;BBBB;CCC;DDDD;EEE;FFF; ... ... reading file line by line till end of file. while reading each line output format should be . i need to print only specific columns let say 5th... (2 Replies)
Discussion started by: rocking77
2 Replies

9. Shell Programming and Scripting

How to print last column of line

Hello folks, Please guide me i have a file file.txt that have below text. PETER JOHN peter@example.com John Col john@example.com Sara Paul sara@example.com I just want to extract only email address list. (5 Replies)
Discussion started by: learnbash
5 Replies

10. Shell Programming and Scripting

print line before column 1 transitions

I have a space delimited text file with thousands of lines, COLA.TXT that looks for example like this: AA 123 456 789 AA 987 987 987 AA 987 988 888 AA 999 999 999 B 123 456 789 B 111 111 111 CCD 123 456 789 CCD 321 654 987 CCD EE 11 11 EE EE EE 00 00 00 EE Anyway, I need a script... (2 Replies)
Discussion started by: ajp7701
2 Replies
Login or Register to Ask a Question