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
# 15  
Old 02-03-2006
Gaurav,

Sorry, but the same errors occur with new file name

Kingkong
# 16  
Old 02-03-2006
Kingkong

did you removed the previous script named max
if not delete it and then run the new script

Gaurav
# 17  
Old 02-03-2006
Gaurav,

I renamed it, so the script max is gone.

Kingkong
# 18  
Old 02-03-2006
Ok this is my last try,

Copy the exact script in solution.sh. Keep your data in file.txt.
And in the script change the variable name max to something else, may be maximum everywhere.
Then run the script with set -x enabled and post the output here.

Do it one more time, we will do it and find the error man.

Gaurav
# 19  
Old 02-03-2006
Gaurav,

Sorry, but it still does not work.

Output:

+ solution.sh
+ maximum=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 ']'
./solution.sh: line 7: [: 34.5: integer expression expected
+ '[' 68.7 -gt 0 ']'
./solution.sh: line 10: [: 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 ']'
./solution.sh: line 7: [: 22.0: integer expression expected
+ '[' 40.1 -gt 0 ']'
./solution.sh: line 10: [: 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 ']'
./solution.sh: line 7: [: 11.3: integer expression expected
+ '[' 34.8 -gt 0 ']'
./solution.sh: line 10: [: 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

Kingkong
# 20  
Old 02-03-2006
Hi Kingkong,

Sorry Buddy, Now we'll wait for someone to resolve the problem.
Just do one thing paste your entire code here for the reference of Gurus and if possible specify the exact problem.

Regards,
Gaurav
# 21  
Old 02-03-2006
Gaurav,

Problem:
I want to find the maximum value in column 2 and 3 in file.txt (given below) and print the line containing this value. Hence, the following line should be printed (because 68.7 is maximum)

1001 34.5 68.7 67

Script named solution.sh:

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

grep $maximum file.txt


file.txt:

1001 34.5 68.7 67
1002 22.0 40.1 32
1003 11.3 34.8 45

Kingkong
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