- SORT command in Linux/Unix with examples
- sort(1) — Linux man page
- Synopsis
- Description
- Author
- Reporting Bugs
- Copyright
- See Also
- Linux and Unix sort command tutorial with examples
- Tutorial on using sort, a UNIX and Linux command for sorting lines of text files. Examples of alphabetical sorting, reverse order sorting, sorting by number and mixed case sorting.
- Table of contents
- What is the sort command?
- How to sort alphabetically
- How to sort in reverse order
- How to sort by number
- How to sort mixed-case text
- How to check if a file is already sorted
- How to sort and remove duplicates
- How to sort by month
- How to sort by items not at the beginning of the line
- How to sort a CSV file
- Further reading
- Recent Posts
- About the author
- How to Sort Files in Linux using Sort Command
- 1. Perform Numeric Sort using -n option
- 2. Sort Human Readable Numbers using -h option
- 3. Sort Months of an Year using -M option
- 4. Check if Content is Already Sorted using -c option
- 5. Reverse the Output and Check for Uniqueness using -r and -u options
- 6. Selectively Sort the Content, Customize delimiter, Write output to a file using -k, -t, -o options
SORT command in Linux/Unix with examples
SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.
- SORT command sorts the contents of a text file, line by line.
- sort is a standard command-line program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order.
- The sort command is a command-line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month, and can also remove duplicates.
- The sort command can also sort by items not at the beginning of the line, ignore case sensitivity, and return whether a file is sorted or not. Sorting is done based on one or more sort keys extracted from each line of input.
- By default, the entire input is taken as the sort key. Blank space is the default field separator.
The sort command follows these features as stated below:
- Lines starting with a number will appear before lines starting with a letter.
- Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
- Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.
Examples
Suppose you create a data file with name file.txt:
Sorting a file: Now use the sort command
Syntax :
Note: This command does not actually change the input file, i.e. file.txt.
Sort function with mix file i.e. uppercase and lower case: When we have a mix file with both uppercase and lowercase letters then first the upper case letters would be sorted following with the lower case letters.
Example:
Create a file mix.txt
Now use the sort command
Options with sort function:
1. -o Option: Unix also provides us with special facilities like if you want to write the output to a new file, output.txt, redirects the output like this or you can also use the built-in sort option -o, which allows you to specify an output file.
Using the -o option is functionally the same as redirecting the output to a file.
Note: Neither one has an advantage over the other.
Example: The input file is the same as mentioned above.
Syntax:
2. -r Option: Sorting In Reverse Order: You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default.
Example: The input file is the same as mentioned above.
Syntax :
3. -n Option: To sort a file numerically used –n option. -n option is also predefined in Unix as the above options are. This option is used to sort the file with numeric data present inside.
Example :
Let us consider a file with numbers:
Syntax:
4. -nr option: To sort a file with numeric data in reverse order we can use the combination of two options as stated below.
Example: The numeric file is the same as above.
Syntax :
5. -k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option.
Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.
Example :
Let us create a table with 2 columns
Syntax :
6. -c option: This option is used to check if the file given is already sorted or not & checks if a file is already sorted pass the -c option to sort. This will write to standard output if there are lines that are out of order. The sort tool can be used to understand if this file is sorted and which lines are out of order
Example :
Suppose a file exists with a list of cars called cars.txt.
Syntax :
7. -u option: To sort and remove duplicates pass the -u option to sort. This will write a sorted list to standard output and remove duplicates.
This option is helpful as the duplicates being removed give us a redundant file.
Example: Suppose a file exists with a list of cars called cars.txt.
Syntax :
8. -M Option: To sort by month pass the -M option to sort. This will write a sorted list to standard output ordered by month name.
Example:
Suppose the following file exists and is saved as months.txt
Using The -M option with sort allows us to order this file.
Application and uses of sort command:
- It can sort any type of file be it table file text file numeric file and so on.
- Sorting can be directly implemented from one file to another without the present work being hampered.
- Sorting of table files on the basis of columns has been made way simpler and easier.
- So many options are available for sorting in all possible ways.
- The most beneficial use is that a particular data file can be used many times as no change is made in the input file provided.
- Original data is always safe and not hampered.
Источник
sort(1) — Linux man page
Synopsis
Description
Write sorted concatenation of all FILE(s) to standard output.
Mandatory arguments to long options are mandatory for short options too. Ordering options: -b, —ignore-leading-blanks ignore leading blanks -d, —dictionary-order consider only blanks and alphanumeric characters -f, —ignore-case fold lower case to upper case characters -g, —general-numeric-sort compare according to general numerical value -i, —ignore-nonprinting consider only printable characters -M, —month-sort compare (unknown) -h, —human-numeric-sort compare human readable numbers (e.g., 2K 1G) -n, —numeric-sort compare according to string numerical value -R, —random-sort sort by random hash of keys —random-source=FILE get random bytes from FILE -r, —reverse reverse the result of comparisons —sort=WORD sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V -V, —version-sort natural sort of (version) numbers within text
Other options: —batch-size=NMERGE merge at most NMERGE inputs at once; for more use temp files -c, —check, —check=diagnose-first check for sorted input; do not sort -C, —check=quiet, —check=silent like -c, but do not report first bad line —compress-program=PROG compress temporaries with PROG; decompress them with PROG -d —files0-from=F read input from the files specified by NUL-terminated names in file F; If F is — then read names from standard input -k, —key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line) -m, —merge merge already sorted files; do not sort -o, —output=FILE write result to FILE instead of standard output -s, —stable stabilize sort by disabling last-resort comparison -S, —buffer-size=SIZE use SIZE for main memory buffer -t, —field-separator=SEP use SEP instead of non-blank to blank transition -T, —temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories -u, —unique with -c, check for strict ordering; without -c, output only the first of an equal run -z, —zero-terminated end lines with 0 byte, not newline —help display this help and exit —version output version information and exit
POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.
SIZE may be followed by the following multiplicative suffixes: % 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.
With no FILE, or when FILE is -, read standard input.
*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.
Author
Reporting Bugs
Copyright
See Also
should give you access to the complete manual.
Источник
Linux and Unix sort command tutorial with examples
Tutorial on using sort, a UNIX and Linux command for sorting lines of text files. Examples of alphabetical sorting, reverse order sorting, sorting by number and mixed case sorting.
Estimated reading time: 5 minutes
Table of contents
What is the sort command?
The sort command is a command line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month and can also remove duplicates. The sort command can also sort by items not at the beginning of the line, ignore case sensitivity and return whether a file is sorted or not.
How to sort alphabetically
The sort tool will sort lines alphabetically by default. Running sort filename writes the contents of the filename in alphabetical order to standard output.
Suppose a file exists with the following list of metal bands that needs to be sorted in alphabetical order. The file is saved as bands.txt .
The sort command allows us to sort the file alphabetically.
How to sort in reverse order
To sort in reverse order pass the -r option to sort . This will sort in reverse order and write the result to standard output.
Using the same list of metal bands from the previous example this file can be sorted in reverse order with the -r option.
How to sort by number
To sort by number pass the -n option to sort . This will sort from lowest number to highest number and write the result to standard output.
Suppose a file exists with a list of items of clothing that has a number at the start of the line and needs to be sorted numerically. The file is saved as clothes.txt .
By passing sort the -n option the file is ordered numerically.
How to sort mixed-case text
To sort mixed-case text pass the -f option to sort . This will ignore case sensitivity when sorting and write the result to standard output.
If a file has uppercase and lowercase content sort will order uppercase first. Suppose a file exists with a list of names in a file called names.txt .
By default the sort tool will sort uppercase characters first.
To sort and ignore case use the -f option.
How to check if a file is already sorted
To check if a file is already sorted pass the -c option to sort . This will write to standard output if there are lines that are out of order.
Suppose a file exists with a list of cars called cars.txt .
The sort tool can be used to understand if this file is sorted and which lines are out of order.
If there is no output then the file is considered to be already sorted.
How to sort and remove duplicates
To sort and remove duplicates pass the -u option to sort . This will write a sorted list to standard output and remove duplicates.
Suppose a file exists with a list of breakfast cereals to sort. This file contains a number of duplicates. This is saved in the file breakfast.txt .
By using the -u option this file can be sorted and stripped of duplicates.
How to sort by month
To sort by month pass the -M option to sort . This will write a sorted list to standard output ordered by month name.
Suppose the following file exists and is saved as months.txt .
Using The -M option with sort allows us to order this file.
How to sort by items not at the beginning of the line
To sort by items not at the beginning of the line pass the -k option to sort along with a number of value of the field to sort on. This will write the result to standard output.
Suppose a file exists with a list of orders that is saved as orders.txt .
The file needs to be sorted by the name of the company that placed them. By using the -k option and passing it a number of the key this can be achieved.
How to sort a CSV file
To sort by a delimiter pass the -t option to sort along with the delimiter value. For a CSV file this would be , . This can be combined with the -k option to sort on fields within a CSV. The result will be written to standard output.
Suppose a file exists with a list of cheeses that is saved as cheese.csv .
The file may be sorted by the name of the cheese using a combination of the -k and -t options.
To sort on the most expensive cheese the numeric and reverse options can be used.
Further reading
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Recent Posts
About the author
George Ornbo is a UK based human.
He is interested in people, music, food and writing. In a previous version of himself he wrote books on technology.
Источник
How to Sort Files in Linux using Sort Command
Sort command is helpful to sort/order lines in text files. 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.
Sort Command Syntax:
For example, here is a test file:
And, here is what you get when sort command is executed on this file without any option. It sorts lines in test file and displays sorted output.
1. Perform Numeric Sort using -n option
If we want to sort on numeric value, then we can use -n or –numeric-sort option.
Create the following test file for this example:
The following sort command sorts lines in test file on numeric value in first word of line and displays sorted output.
2. Sort Human Readable Numbers using -h option
If we want to sort on human readable numbers (e.g., 2K 1M 1G), then we can use -h or –human-numeric-sort option.
Create the following test file for this example:
The following sort command sorts human readable numbers (i.e 1K = 1 Thousand, 1M = 1 Million, 1G = 1 Giga, 1T = 1 Tera) in test file and displays sorted output.
3. Sort Months of an Year using -M option
If we want to sort in the order of months of year, then we can use -M or –month-sort option.
Create the following test file for this example:
The following sort command sorts lines in test file as per month order. Note, lines in file should contain at least 3 character name of month name at start of line (e.g. jan, feb, mar). If we will give, ja for January or au for August, then sort command would not consider it as month name.
4. Check if Content is Already Sorted using -c option
If we want to check data in text file is sorted or not, then we can use -c or –check, –check=diagnose-first option.
Create the following test file for this example:
The following sort command checks whether text file data is sorted or not. If it is not, then it shows first occurrence with line number and disordered value.
5. Reverse the Output and Check for Uniqueness using -r and -u options
If we want to get sorted output in reverse order, then we can use -r or –reverse option. If file contains duplicate lines, then to get unique lines in sorted output, “-u” option can be used.
Create the following test file for this example:
The following sort command sorts lines in test file in reverse order and displays sorted output.
The following sort command sorts lines in test file in reverse order and removes duplicate lines from sorted output.
6. Selectively Sort the Content, Customize delimiter, Write output to a file using -k, -t, -o options
If we want to sort on the column or word position in lines of text file, then “-k” option can be used. If we each word in each line of file is separated by delimiter except ‘space’, then we can specify delimiter using “-t” option. We can get sorted output in any specified output file (using “-o” option) instead of displaying output on standard output.
Create the following test file for this example:
The following sort command sorts lines in test file on the 3rd word of each line and displays sorted output.
Here, several options are used altogether. In test file, words in each line are separated by delimiter ‘|’. It sorts lines in test file on the 2nd word of each line on the basis of numeric value and stores sorted output into specified output file.
The contents of output file are shown below.
Источник