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.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.


Bash - sort lines
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂