Linux sort

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

 Linux sort

The 'sort' command sorts the file content in an alphabetical order.

Syntax:

sort <fileName> 

Example:

sort weeks.txt

Linux Sort Filters1

Look at the above snapshot, the 'sort' command has sorted the file 'weeks.txt' in alphabetical order.


To Sort A Column

If a file has more than one column, column number is used to sort a specific column.

Syntax:

sort -k<columnNumber> <fileName> 

Example:

  1. sort -k1 states.txt  
  2. ort -k2 states.txt  

Linux Sort Filters2

Look at the above snapshot, we have sorted column 1 and 2.


Numeric Sorting

Numeric sorting is different from alphabetical sorting. For numeric sorting option 'n' is used along with the column number if required.

Syntax:

sort -n -k<columnNumber> <fileName>  

Example:

sort -n -k2 marks.txt  

 

Linux Sort Filters3