Skip to content
Advertisement

How to find a postion of the lowest value in the file?

How to output position of a line with the lowest value inside?

I have a file with data like below:

2233|charles harris  |g.m.     |sales     |12/12/52| 90000
9876|bill johnson    |director |production|03/12/50|130000
5678|robert dylan    |d.g.m.   |marketing |04/19/43| 85000
2365|john woodcock   |director |personnel |05/11/47|120000
5423|barry wood      |chairman |admin     |08/30/56|160000
1006|gordon lightfoot|director |sales     |09/03/38|140000

I need to find a lowest value from the last column and return its position

Advertisement

Answer

You can use this command in order to get only the line number where the value is shown(if the value appears more than one time then it will give several line numbers):

var=$(cut -d "|" -f 6 filename|sort -nr|tail -n1|grep -w -n -f /dev/stdin filename|awk -F ":" '{print $1}');cut -d "|" -f 6 filename |grep -w -n $var filename|awk -F ":" '{print $1}'

in order to see the line with the lowest value and it’s line number use this command:

var=$(cut -d "|" -f 6 filename|sort -nr|tail -n1|grep -w -n -f /dev/stdin filename|awk -F ":" '{print $1}');cut -d "|" -f 6 filename |grep -w -n $var filename
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement