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
# 1  
Old 02-03-2006
Print the line containing the maximum value in a column

Dear all!

I want to find the maximum value in two specific columns with numbers, and then print the entire line containing this value. The file may look like:

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

I want to find the maximum value within column 2 and 3, which means I want the following output (because the maximum value is 68.7):

1001 34.5 68.7 67

I hope there is someone able to help me on this!

Thanks!
# 2  
Old 02-03-2006
Hey kingkong ,

Heres the solution

Quote:
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
max=$fir
fi
if [ $sec -gt $max ];then
max=$sec
fi
done

grep $max file.txt
where your data is in file.txt
do let me know if it works
cheers ,

Gaurav
# 3  
Old 02-03-2006
Hey Gaurav!

First of all, many thanks for your time!

I do not get the code to work as desired. The first error message I get reads "integer expression expected". When I try again with column 2 and 3 given as integers, all lines within file.txt are printed, and not only the line containing the maximum value. I would really appreciate if you have som further comments to this.

Best regards
Kingkong
# 4  
Old 02-03-2006
>t.awk

Code:
BEGIN {
val=0;
line=1;
}
{
if( $2 > $3 )
{
   if( $2 > val )
   {
      val=$2;
      line=$0;
   }
}
else
{
   if( $3 > val )
   {
      val=$3;
      line=$0;
   }
}
}
END{
print line
}

Code:
awk -f t.awk filename

# 5  
Old 02-03-2006
Hello Matrixmadhan!

The code works excellent! Thank you very much and best regards!

Kingkong
# 6  
Old 02-03-2006
Hi Kingkong the code is working fine for me as you can observe below

Quote:
cat soln.sh
#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
max=$fir
fi
if [ $sec -gt $max ];then
max=$sec
fi
done

grep $max file.txt

gaurav]$cat file.txt
1001 34.5 68.7 67
1002 22.0 40.1 32
1003 11.3 34.8 45

/gaurav]$./soln.sh
1001 34.5 68.7 67
I used your data only as input.

Can some guru please throw some light on it. Is this due to differnce of shell.
I am using sh. Kingkong which shell you are using.

Regards,
Gaurav
# 7  
Old 02-03-2006
Hey Gaurav, and thanks again!

I use bash shell. However, by typing #!/bin/sh at the top of the script a thought I would convert to sh shell. I tried this, but the code does still not work for me.

Best regards,
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