Bash - how to sort lines?

7 points
Created by:

Sort operation can be achieved with sort command.

1. Sorting by rows:

$ sort path/to/textual/file

2. Reversed sorting by rows:

sort -r path/to/textual/file

3. Number sorting:

sort –n path/to/textual/file

4. Sorted by first column (pipe used):

cat /etc/passwd | cut -d : -f 1 | sort

Note: delimiter set as colon.

5. Sorted by third and fourth columns (pipe used):

cat path/to/textual/file | sort -k 3 -k 4 -n

Note: default delimiters are space and tab.

6. Linux user list sorted by id:

cat /etc/passwd | cut -d : -f 1,3 | sort -t ':' -k 2 -n

Note: delimiter set as colon; selected columns are 1st and 3rd; sorting by id.

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.