16

How to find the max value from the column 1 and echo the respective path location from a file which contains n number of records.

$ cat version.log
112030  /opt/oracle/app/oracle/product/11.2.0
121010  /opt/oracle/app/oracle/product/12.1.0

Expected output:

/opt/oracle/app/oracle/product/12.1.0
18

This should work:

awk -v max=0 '{if($1>max){want=$2; max=$1}}END{print want} ' version.log

The -v max=0 sets the variable max to 0, then, for each line, the first field is compared to the current value of max. If it is greater, max is set to the value of the 1st field and want is set to the current line. When the program has processed the entire file, the current value of want is printed.

Edit

I did not test the awk solution earlier and it was really my bad to have provided it. Anyways, the edited version of the answer should work (Thanks to terdon for fixing it) and I tested the below as well.

sort -nrk1,1 filename | head -1 | cut -d ' ' -f3

I am sorting on the first field where,

  • -n specifies numerical sort.
  • -r specifies reverse the sort result.
  • -k1,1 specifies first field for the sorting to occur.

Now, after the sorting I am piping the output and just getting the first result which will give me the numerically highest value of column1 in the result.

Now, I finally pipe it to cut with the delimiter specified as space and printing the -f3 which is the intended output.

Testing

cat filename
112030   /opt/oracle/app/oracle/product/11.2.0
121010   /opt/oracle/app/oracle/product/12.1.0
2312     /hello/some/other/path
3423232  /this/is/really/big/number
342      /ok/not/the/maximum/number
9999899  /Max/number
9767     /average/number

Now, after I run the above command for the input as above, I get the output as,

/Max/number
  • awk max doesn't compare string field numerically. is that why you gave a second approach? – Sajuuk May 24 '18 at 6:15
7

You can do it with AWK.

$ awk '{if(max<$1){max=$1;line=$2}}END{print line}' file 
/opt/oracle/app/oracle/product/12.1.0

Here first column of each line is compared with the variable max (which is initially 0). If the first column has a value greated that max then the second column is stored in the variable line, this continues for each and every line of the file.

At the end of the file END rule is executed to print the line variable.

3
awk '$1 > max { max = $1; output = $2 } END { print output }' version.log
2

sort -k1 -n filename | tail -1 | awk '{print $2}'

  • 1
    This should work but is inefficient, because sorting is overkill when the task is to find a maximum. – jw013 Nov 27 '14 at 5:20
2

Simply you can sort it by using sort command

sort -r version.log | head -n1 | awk '{print $2}'

Output:

/opt/oracle/app/oracle/product/12.1.0
  • you want to print the second value only try this cat version.log | sort -r | head -n1 | awk -F " " '{Print $2}' – Security Beast Nov 27 '14 at 7:34
  • Please don't post screenshots of your terminal. Just paste the text instead. Also, there's no need for cat, sort can take input files directly. – terdon Nov 27 '14 at 13:54
-2
sort -nrk1,1 version.log | head -1 |awk '{print $2}’
  • 2
    This looks like a re-hash of the accepted answer? Please explain how it differ. – Stephen Rauch Dec 16 '17 at 23:44
  • Indeed, at the very least give credit to the original author. – Jeff Schaller Dec 17 '17 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.