Seven interesting Linux Sort commands (2)

Source: Internet
Author: User

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.

  1. $ echo -e "mar\ndec\noct\nsep\nfeb\naug"> month.txt
  2. $ cat month.txt

15. You can use 'M' to sort 'month.txt 'files by month.

  1. $ 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)

  1. $ 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.

  1. $ sort -c sorted.txt

If it returns 0, it indicates that the order of the file has been sorted.

  1. $ 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.

  1. $ 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
  1. $ cat delimiter.txt

It is now sorted based on the first field composed of digits.

  1. $ 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.

  1. $ 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:

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.