Spasitel
Spasitel

Reputation: 169

Sort a file and then print out part of it in original order - Bash

I want to print out 5 users with the highest UID, but the lines have to be in the original order, as they are in /etc/passwd

I am able to do in only in a sorted way:

sort -t":" -k3 -n /etc/passwd | tail -n5

Upvotes: 1

Views: 39

Answers (1)

Jonathan
Jonathan

Reputation: 1402

You could use cat -n to get line numbers along with the output and then do a sort at the end on those line numbers.

cat -n /etc/passwd | sort -t":" -k3 -n | tail -n5 | sort -n

Upvotes: 1

Related Questions