How to sort contents of a file

The sort command is used to sort the contents of a file. You can sort the data in a 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]

sort file

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

sort number file

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

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 


You may want to see
How to use sed command in Linux

sed, short for "stream editor", allows you to filter and transform text. A stream editor is used to perform basic Read more

How to use crontab in Linux

Cron is the system process that will automatically perform tasks for you according to a set schedule. The schedule is Read more

How to extract archive file in Linux

To extract archive file in Linux, you can use the tar command, used for extracting content from the archive files. "tar" Read more

How to Change permission of a file in Linux

chmod command is used to change the permission of a file in Linux. In Linux, there is a set of rules for Read more

Leave a Reply

Your email address will not be published. Required fields are marked *