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!
Hey kingkong ,
Heres the solution
where your data is in file.txt
do let me know if it works
cheers ,
Gaurav
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
>t.awk
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
}
awk -f t.awk filename
Hello Matrixmadhan!
The code works excellent! Thank you very much and best regards!
Kingkong
Hi Kingkong the code is working fine for me as you can observe below
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
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
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
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
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.
I am sure that this will work,
Cheers,
Gaurav
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
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
Hi Gaurav,
I pasted the exact code into a script named max.
Kingkong
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
Gaurav,
Sorry, but the same errors occur with new file name
Kingkong
Kingkong
did you removed the previous script named max
if not delete it and then run the new script
Gaurav
Gaurav,
I renamed it, so the script max is gone.
Kingkong
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
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
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