Yarmiz
Yarmiz

Reputation: 149

In a linux file, how to print the 20 biggest numbers in a column?

I tried to use awk and I can find the biggest number in the 8th column for example. Now I need to find the 20 biggest in the column. Do I use many variables? Here's what I know how to use:

$ awk '$8>max{max=$8}END{print max}' my_file.txt

I'm new to linux and I find it difficult to find answers to problems like this in the manual, so I hope you guys can help me! Thank you! :)

Oh and an other thing: I'll need to change the permissions of all the files containing "18" inside. I tried to use something like this and it doesn't work because I'm missing something (I don't even know if it's possible to write it like that):

grep -r 18 | chmod -R 775

Thank you again for your help!

Upvotes: 1

Views: 169

Answers (2)

legoscia
legoscia

Reputation: 41618

Something like this:

sort -k 8 -n -r my_file.txt | head -20

That is, sort using field 8 as key (-k stands for "key"), using numeric sort (-n), in reverse order (-r), that is, starting with the biggest numbers. Finally, take the first 20 lines with head.

Upvotes: 5

Partha Lal
Partha Lal

Reputation: 541

Have you tried simply sort -nrk8 my_file.txt | head -20?
The n flag makes it a numeric sort, the r reverses it (largest first) and the k8 picks the 8th column.

Upvotes: 5

Related Questions