Saturday, 11 July 2015

How to sort contents of a file

sort command is used to sort the contents of a file. You can sort the data in text file and display the output on the screen, or redirect it to a file. Based on your requirement, sort provides several command line options for sorting data in a text file.

Syntax:
sort [options] filename

Examples:

#1 To sort text file [sort sort_file]

#2 To sort numbers in file, use -n option [sort -n sort_number_file]


#3 To sort months in file, use -M option [sort -M sort_month_file]

Refer below tables for more sort command options:
-b  Ignores leading spaces in each line 
 -d  Uses dictionary sort order. Conisders only spaces and alphanumeric characters in sorting 
 -f  Uses case insensitive sorting. 
 -M  Sorts based on months.  Either mention complete month name or only first 3 letters. Eg: JAN, FEB 
 -n  Uses numeric sorting 
 -R  Sorts the input file randomly. 
 -r  Reverse order sorting 
 -k  Sorts file based on the data in the specified field positions. 
 -u  Suppresses duplicate lines 
 -t  input field separator 





No comments:

Post a Comment