- SORT command in Linux/Unix with examples
- Linux sort command
- Overview
- Syntax
- Options
- Checking For Sorted Order
- Sorting Multiple Files Using The Output Of find
- Comparing Only Selected Fields Of Data
- Using sort And join Together
- Related commands
- 10 Useful Examples of the Sort Command in Linux
- Sort command in Linux
- Examples of the sort command
- 1. Sort in alphabetical order
- 2. Sort on numerical value [option -n]
- 3. Sort in reverse order [option -r]
- 4. Random sort [option -R]
- 5. Sort by months [option -M]
- 6. Save the sorted results to another file
- 7. Sort Specific Column [option -k]
- 8. Sort and remove duplicates [option -u]
- 9. Ignore case while sorting [option -f]
- 10. Sort by human numeric values [option -h]
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.
Источник
Linux sort command
sort sorts the contents of a text file, line by line.
Overview
sort is a simple and very useful command which will rearrange the lines in a text file so that they are sorted, numerically and alphabetically. By default, the rules for sorting are:
- 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.
The rules for sorting can be changed according to the options you provide to the sort command; these are listed below.
Syntax
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) Note |
if you are using the join command in conjunction with sort, be aware that there is a known incompatibility between the two programs — unless you define the locale. If you are using join and sort to process the same input, it is highly recommended that you set LC_ALL to C, which will standardize the localization used by all programs.
Checking For Sorted Order
If you just want to check to see if your input file is already sorted, use the -c option:
If your data is unsorted, you will receive an informational message reporting the line number of the first unsorted data, and what the unsorted data is:
Sorting Multiple Files Using The Output Of find
One useful way to sort data is to sort the input of multiple files, using the output of the find command. The most reliable (and responsible) way to accomplish this is to specify that find produces a NUL-terminated file list as its output, and to pipe that output into sort using the —files0-from option.
Normally, find outputs one file on each line; in other words, it inserts a line break after each file name it outputs. For instance, let’s say we have three files named data1.txt, data2.txt, and data3.txt. find can generate a list of these files using the following command:
This command uses the question mark wildcard to match any file that has a single character after the word «data» in its name, ending in the extension «.txt«. It produces the following output:
It would be nice if we could use this output to tell the sort command, «sort the data in any files found by find as if they were all one big file.» The problem with the standard find output is, even though it’s easy for humans to read, it can cause problems for other programs that need to read it in. Because file names can include non-standard characters, so in some cases, this format will be read incorrectly by another program.
The correct way to format find‘s output to be used as a file list for another program is to use the -print0 option when running find. This terminates each file name with the NUL character (ASCII character number zero), which is universally illegal to use in file names. This makes things easier for the program reading the file list, since it knows that any time it sees the NUL character, it can be sure it’s at the end of a file name.
So, if we run the previous command with the -print0 option at the end, like this:
. it will produce the following output:
You can’t see it, but after each file name is a NUL character. This character is non-printable, so it will not appear on your screen, but it’s there, and any programs you pipe this output to (sort, for example) will see them.
Be careful how you word the find command. It’s important to specify -print0 last; find needs this to be specified after the other options.
Okay, but how do we tell sort to read this file list and sort the contents of all those files?
One way to do it is to pipe the find output to sort, specifying the —files0-from option in the sort command, and specify the file as a dash («—«), which will read from the standard input. Here’s what the command will look like:
. and it will output the sorted data of any files located by find which matches the pattern data?.txt, as if they were all one file. This example is a very powerful function of sort — give it a try.
Comparing Only Selected Fields Of Data
Normally, sort decides how to sort lines based on the entire line: it compares every character from the first character in a line, to the last one.
If, on the other hand, you want sort to compare a limited subset of your data, you can specify which fields to compare using the -k option.
For instance, if you have an input file data.txt With the following data:
. and you sort it without any options, like this:
. you will receive the following output:
. as you can see, nothing was changed from the original data ordering, because of the numbers at the beginning of the line — which were already sorted. However, if you want to sort based on the names, you can use the following command:
This command will sort the second field, and ignore the first. (The «k» in «-k» stands for «key» — we are defining the «sorting key» used in the comparison.)
Fields are defined as anything separated by whitespace; in this case, an actual space character. Our command above will produce the following output:
. which is sorted by the second field, listing the lines alphabetically by name, and ignoring the numbers in the sorting process.
You can also specify a more complex -k option. The complete positional argument looks like this:
. where POS1 is the starting field position, and POS2 is the ending field position. Each field position, in turn, is defined as:
. where F is the field number and C is the character within that field to begin the sort comparison.
So, let’s say our input file data.txt contains the following data:
. we can sort by seniority if we specify the third field as the sort key:
. this produces the following output:
Or, we can ignore the first three characters of the third field, and sort solely based on title, ignoring seniority:
We can also specify where in the line to stop comparing. If we sort based on only the third-through-fifth characters of the third field of each line, like this:
. sort will see only the same thing on every line: «.De» . and nothing else. As a result, sort will not see any differences in the lines, and the sorted output will be the same as the original file:
Using sort And join Together
sort can be especially useful when used in conjunction with the join command. Normally join will join the lines of any two files whose first field match. Let’s say you have two files, file1.txt and file2.txt. file1.txt contains the following text:
. and file2.txt contains the following:
If you’d like sort these two files and join them, you can do so all in one command if you’re using the bash command shell, like this:
Here, the sort commands in parentheses are each executed, and their output is redirected to join, which takes their output as standard input for its first and second arguments; it is joining the sorted contents of both files and gives results similar to the below results.
Related commands
comm — Compare two sorted files line by line.
join — Join the lines of two files which share a common field of data.
uniq — Identify, and optionally filter out, repeated lines in a file.
Источник
10 Useful Examples of the Sort Command in Linux
Sort command in Linux
The sort command arranges text lines in useful ways. This simple tool can help you quickly sort information from the command line.
Syntax
You should note a few thing:
- When you use sort without any options, the default rules are enforced. It helps to understand the default rules to avoid unexpected outcomes.
- When using sort, your original data is safe. The results of your input are displayed on the command line only. However, you can specify output to a separate file if you wish. More on that later.
- Sort was originally designed for use with ASCII characters. I did not test for this, but it is possible that different encodings may produce unexpected results.
The default rules in the sort command
These are the default rules when using sort. The first few examples will clarify how these priorties are managed. Then we will look at specialized options.
- numbers > letters
- lowercase > uppercase
Examples of the sort command
Let me show you some examples of sort command that you can use in various situations.
1. Sort in alphabetical order
The default sort command makes it easy to view information in alphabetical order. No options are necessary and even with mixed-case entries, A-Z sorting works as expected.
I am going to use a sample text file named filename.txt and if you view the content of the file, this is what you’ll see:
Now if you use sort command on it:
Here’s the alphabetically sorted output:
2. Sort on numerical value [option -n]
Let’s take the same list we used for the previous example and sort in numerical order. In case you were wondering, the list reflects the most popular Linux distributions (July, 2019) according to distrowatch.com.
I will modify the contents of the file so that the items are numbered, but out of order as shown below.
After sorting, the result is:
Looks good, right? Can you rely on this method to arrange your data accurately, though? Probably not. Let’s look at another example to find out why.
Here’s my new sample text:
Now, if I use the sort command without any options, here’s what I get:
NOTE: Numbers are sorted by their leading characters only.
When you add the -n option, the numerical value of the string is now being evaluated rather than only the first character. Now, you can see below that our list is properly sorted.
Now you’ll have the correctly sorted output:
3. Sort in reverse order [option -r]
For this one, I am going to use our distro list again. The reverse function is self-explanatory. It will reverse the order of whatever content you have in your file.
And here you have the output text in reverse order:
4. Random sort [option -R]
If you accidentally mashed your shift key while attempting the reverse function, you might have gotten some strange results. -R rearranges output in randomized order.
Here’s the randomly sorted output:
5. Sort by months [option -M]
Sort also has built in functionality to arrange by month. It recognizes several formats based on locale-specific information. I tried to demonstrate some unqiue tests to show that it will arrange by date-day, but not year. Month abbreviations display before full-names.
Here is the sample text file in this example:
Let’s sort it by months using the -M option:
Here’s the output you’ll see:
6. Save the sorted results to another file
As I mentioned earlier, sort does not change the original file by default. If you need to save the sorted content, it can be done.
For this example, I’ve created a new file where I want the sorted information to be printed and saved with the name filename_sorted.txt.
Caution: If you try to direct your sorted data to the same file, it will erase the contents of your file.
If you use cat command on the output file, this will be its contents:
7. Sort Specific Column [option -k]
If you have a table in your file, you can use the -k option to specify which column to sort. I added some arbitrary numbers as a third column and will display the output sorted by each column. I’ve included several examples to show the variety of output possible. Options are added following the column number.
This will sort the text on the second column in alphabetical order:
This will sort the text by the numerals on the third column.
Same as the above command just that the sort order has been reversed.
8. Sort and remove duplicates [option -u]
If you have a file with potential duplicates, the -u option will make your life much easier. Remember that sort will not make changes to your original data file. I chose to create a new file with just the items that are duplicates. Below you’ll see the input and then the contents of each file after the command is run.
Here’s the output files sorted and without duplicates.
9. Ignore case while sorting [option -f]
Many modern distros running sort will implement ignore case by default. If yours does not, adding the -f option will produce the expected results.
Here’s the output where cases are ignored by the sort command:
10. Sort by human numeric values [option -h]
This option allows the comparison of alphanumeric values like 1k (i.e. 1000).
Here’s the sorted output:
I hope this tutorial helped you get the basic usage of the sort command in Linux. Sort command is often used in conjugation with the uniq command in Linux for uniquely sorting text files.
If you have some cool sort trick, why not share it with us in the comment section?
Источник