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
Best Answer
This should work:
The
-v max=0
sets the variablemax
to0
, then, for each line, the first field is compared to the current value ofmax
. If it is greater,max
is set to the value of the 1st field andwant
is set to the current line. When the program has processed the entire file, the current value ofwant
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.I am
sort
ing on the first field where,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
Now, after I run the above command for the input as above, I get the output as,