CentOS Linux Server Guide

Sorting file contents in CentOS Linux

Sort command is used to sort the the contents of a file.

  • The standard output of the sort command will be displayed on screen. However, you can redirect the output to a file.

  • Sorting fields can be specified by field numbers.

  • The field number of the first field is always start with 0.

  • If more than one file is specified, it will merge the files by sorting them together.

 

Command and Options:

sort  [options] filename(s)

 

Option:

 
Options   Description
-r   reverse the sorting order
-f   ignore uppercase and lowercase differences
-n   sort in numeric order
-k alternative syntax for specifying sorting keys, instead of using +num
-n sort in numerical order
+num or -num specify starting and ending positions
-t character use character as the field separator
-c check whether the given files are already sorted
-m merge the given files by sorting them as a group
-b ignore leading blanks when finding sort keys in each line
-u displaying the unique character, displaying the contents not more than 1 time

 

Example:

 

The the following example will sort the given file A.log
sort A.log

 

The following example will do reverse sorting on the content of A.log
sort -r A.log

 

The following example will sort the content of A.log and the redirect the output (saved) to B.log
sort A.log > B.log

 

The following example will sort the field 2 to filed 10 of A.log
sort +2 -11 A.log

 

The following example, the sort command will merge the files of A.log and C.log by sorting them together
sort A.log C.log

 

Back to Managing Files and Folders in CentOS Linux

 



Copyright 2011 http://www.1a-centosserver.com All Rights Reserved

All trademarks are the property of their respective owners.

Contact Us | Terms of Use | Privacy Policy