3

I have a txt file as follows

 1  8.92
 2  9.99
 3  9.57
 4  9.63
 5  9.57
 6  9.51
 7  7.84
 8  7.95
 9  9.44
10  9.58
11  9.94
12  10.20
13  9.35

I need the line number of the highest value. How can I get that? e.g. the answer for this data will be

12 10.20

  • sort -rn test.txt | awk '!x[$2]++' sort -k2 -n test1.txt | tail -1 | awk '{print $2}' – user210915 Jan 17 '17 at 12:46
  • Are the line numbers part of the input file or not? Also, please edit your question to add extra information. it is hard to read and easy to miss in the comments. – terdon Jan 17 '17 at 12:47
  • yes or no. I need both. Line numbers may be part of the file or may not be. – user210915 Jan 17 '17 at 12:48
1

After copy-paste your example, I did it with:

sort -k2 -nr filename.txt | head -1
  • Need to use the -n -r flags for sort. Add "14 100.1" to the data to verify – glenn jackman Jan 17 '17 at 13:39
  • @glennjackman Just did it with and without -n -r and the output is correct (100.1 as highest value) in both cases. Perhaps it depends on the sort implementation? – Zumo de Vidrio Jan 17 '17 at 13:56
  • 1
    hmm. may depend on your locale. – glenn jackman Jan 17 '17 at 14:03
  • @glennjackman Edited anyway. – Zumo de Vidrio Jan 17 '17 at 14:22
3

To avoid sorting, you could do:

awk 'NR == 1 || $2 > max {number = $1; max = $2}
     END {if (NR) print number, max}' < file

or if the input only contains one column and you want to know the number of the line that has the maximum value:

awk 'NR == 1 || $1 > max {number = NR; max = $1}
     END {if (NR) print number, max}' < file
  • The OP wants the line number though, not just the line. – terdon Jan 17 '17 at 15:24
  • @terdon, from what I understand of the question, the line number is the first column in the file. That would seem to be confirmed by the fact that Zumo's answer was accepted. – Stéphane Chazelas Jan 17 '17 at 15:27
  • Oh. Huh, fair enough. I had assumed the OP was adding them for our convenience. – terdon Jan 17 '17 at 15:28
0

I did it with sort -rn -k2 test, where test is the name of the file containing your example data.

12 10.20
7 7.84
8 7.95
1 8.92
13 9.35
9 9.44
6 9.51
3 9.57
5 9.57
10 9.58
4 9.63
11 9.94
2 9.99

And you can get the line number with:

sort -rn -k2 test | awk '{print $2}' | grep $(head -1) -n test

output:

12:12 10.20
  • you're right, I've edited my answer – 13dimitar Jan 17 '17 at 12:59
  • 1
    You're sorting lexically so "14 100.1" will not show up as the highest value. You need to pass sort the -n -r options to sort numerically – glenn jackman Jan 17 '17 at 13:38
  • 1
    I'll be damned! You're quite right, apparently the pipe will pass data to head before running the grep. Huh. Fair enough, my bad. – terdon Jan 17 '17 at 15:05
  • 1
    @don_crissti I was equally certain but, it turns out, we're the ones who should test. Apparently, the $(head -1) is taking input from the pipe and successfully searches for the first line of output from sort in the test file. – terdon Jan 17 '17 at 15:06
  • 1
    @don_crissti sure, and it also needs to process the file twice. I'm not saying this is a good approach. Just pointing out that the | grep $(head -1) bit does seem to work in bash, which suggests that Dimitar didn't deserve our scathing remarks. – terdon Jan 17 '17 at 16:56

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.