In the last article, we have explored a number of examples of the sort command, if you missed the article, you can click on the following link to read. Today's article as the article continues, will discuss the remaining usage of the sort command, together with the previous one as Linux 'sort' a complete guide to the command.
1, useful example Linux Sort command (a)
Before we go further, to create a text document 'month.txt', and the data on a given filling inside.
$ Echo -e "mar \ ndec \ noct \ nsep \ nfeb \ naug"> month.txt
$ Cat month.txt
2, by using the 'M' option to 'month.txt' files sorted by month order.
$ Sort -M month.txt
Note: 'sort' command requires at least three characters to confirm the name of the month.
3, easy to organize your data into human-readable form, such as 1K, 2M, 3G, 2T, there's a K, G, M, T on behalf of one thousand, trillion, Kyrgyzstan, ladder. (LCTT Annotation: here is wrong command, ls command should increase -h parameter, the diameter corrections)
$ Ls -lh / home / $ USER | sort -h -k5
4, in the last article, we created in the example 4 a file named 'sorted.txt', creating a 'lsl.txt' in Example. 'Sorted.txt' already sorted, and 'lsl.txt' yet. Let's use the sort command to check whether two files are already sorted.
$ Sort -c sorted.txt
Check File is Sorted
If it returns 0, the file is already sorted.
$ Sort -c lsl.txt
Report disorder. Contradictions ......
5, if the delimiter is a space between words, sort command automatically something new space after the word as a unit, if the delimiter is not a space it?
Consider a text file, the contents of which may consist of any sign, except space separated, for example, '|' '.', '\', '+', And so on ......
+ Create a delimiter text file. Using the 'cat' command to view the contents of the file.
$ Echo -e "21 + linux + server + production \ n11 + debian + RedHat + CentOS \ n131 + Apache + Mysql + PHP \ n7 + Shell Scripting + python + perl \ n111 + postfix + exim + sendmail"> delimiter.txt
$ Cat delimiter.txt
Now based on the first domain consisting of numbers to be sorted.
$ Sort -t '+' - nk1 delimiter.txt
Then on the fourth non-numeric domain order.
If delimiter is a tab that you need in the '+' position with $ '\ t' instead of, as in the example below.
6, on the main user directory using the result 'ls -l' command obtained based on the fifth column ( 'file size') were arranged in a random order.
$ Ls -l / home / avi / | sort -k5 -R
Every time you run the above script, you get the results may be different, because the results are randomly generated.
As I mentioned in the rules of the article 2 of said --sort command line will start with a lowercase letter came in the front row of capital letters began. Look at cases on article 3, the string 'laptop' in 'LAPTOP' before appear.
7, how to override the default sort priority? Prior to this we need to first set the value of the environment variable LC_ALL to C. At the command line prompt to run the following bar code.
$ Export LC_ALL = C
Then a non-default priority manner 'tecmint.txt' file reordered.
$ Sort tecmint.txt
Sort priority coverage
Do not forget to resemble the results obtained in example 3 for comparison, and you can use the '-f', also known as '-ignore-case' (ignore case) option to obtain more orderly output.
$ Sort -f tecmint.txt
8, to two input files 'sort', and then connect them in a row!
We created two text documents 'file1.txt' and 'file2.txt', and fill it with data, as shown below, and use the 'cat' command to view the contents of the file.
$ Echo -e "5Reliable \ n2Fast \ n3Secure \ n1 open-source \ n4 customizable"> file1.txt
$ Cat file1.txt
With the following data to populate 'file2.txt'.
$ Echo -e "3RedHat \ n1Debian \ n5Ubuntu \ n2Kali \ n4Fedora"> file2.txt
$ Cat file2.txt
Now we have two sort files and connect.
$ Join <(sort -n file1.txt) <(sort file2.txt)
The entire contents of my argument here, I hope to keep in touch with you, I hope you always come around. Feedback in the comments below it.