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.

Native Advertising
50 000 ad impressions - 449$
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂