Seven interesting Linux Sort commands (2)
In the previous article, we have discussed multiple examples of the sort command. If you miss this article, you can click the link below to read it. As part of the previous article, this article will discuss the remaining usage of the sort command. Together with the previous article, it will serve as a complete guide to the Linux 'sort 'command.
- 14 useful examples of Linux Sort commands (1)
Before continuing, we will first create a Chinese file 'month.txt 'and fill in the data given last time.
$ echo -e "mar\ndec\noct\nsep\nfeb\naug"> month.txt
$ cat month.txt
15. You can use 'M' to sort 'month.txt 'files by month.
$ sort -M month.txt
Note: The 'sort 'command requires at least three characters to confirm the month name.
16. Organize the data into a form that is convenient for people to read, such as 1 K, 2 M, 3G, 2 T, here, K, G, M, and T represent thousands, megabytes, Kyrgyzstan, and ladders. (LCTT note: the command here is incorrect. The-h parameter should be added to the ls command to change the path)
$ ls -lh /home/$USER | sort -h -k5
17. comment '. 'Sorted.txt' has been sorted, but 'lsl.txt 'is not. Let's use the sort command to check whether the two files are sorted.
$ sort -c sorted.txt
If it returns 0, it indicates that the order of the file has been sorted.
$ sort -c lsl.txt
The report is unordered. There are contradictions ......
18. If the delimiter between texts is space, the sort command automatically treats the space after the space as a new text unit. What if the separator is not space?
In such a text file, the content can be separated by any symbols except spaces, such as '|', '\', '+ ......
Create a text file with the + separator. Run the 'cat' command to view the file content.
$ 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
It is now sorted based on the first field composed of digits.
$ sort -t '+'-nk1 delimiter.txt
Then sort the data based on the fourth non-numeric domain.
If the Delimiter is a Tab character, you must replace it with $ '\ t' at the' + 'position, as shown in the preceding example.
19. Run the 'LS-l' command in the directory of the primary user to sort the results in an out-of-order based on the Fifth Column ('file size.
$ ls -l /home/avi/| sort -k5 -R
Every time you run the above script, you may get different results because the results are randomly generated.
As rule 2 mentioned in the previous article said, the sort command ranks the rows starting with lowercase letters before the rows starting with uppercase letters. Take a look at Example 3 in the previous article. The string 'laptop' appears before 'laptop.
For more details, please continue to read the highlights on the next page: