14 useful examples of Linux Sort commands (1)

Source: Internet
Author: User

14 useful examples of Linux Sort commands (1)

Sort is a Linux program used to Sort the content of one or more text files. The Sort command uses space as the field separator and splits a line into multiple keywords to Sort the files. Note that unless you redirect the output to a file, the Sort command does not actually Sort the file content (that is, the file content is not modified), but outputs the File Content in order.

The goal of this article is to use 14 practical examples to give you a deeper understanding of how to use the sort command in Linux.

1. First, we will create a Sequence File (tecmint.txt) for executing the 'sort' command ). The working path is '/home/$ USER/Desktop/tecmint '.

The '-e' option in the following command will enable' \ 'escape and parse' \ n' into line breaks.

  1. $ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop"> tecmint.txt

2. Before learning the 'sort 'command, let's take a look at the file content and display method.

  1. $ cat tecmint.txt

3. Now, use the following command to sort the file content.

  1. $ sort tecmint.txt

Note: The above command does not actually sort the file content, but simply outputs the content in an ordered manner.

4. Contents of the hosts file.

  1. $ sort tecmint.txt > sorted.txt
  2. $ cat sorted.txt

5. Run the cat command to view the file content.

  1. $ sort -r tecmint.txt > reversesorted.txt
  2. $ cat reversesorted.txt

6. Create a new file (lsl.txt) with the output of the 'LS-l' command in the home directory.

  1. $ ls -l /home/$USER >/home/$USER/Desktop/tecmint/lsl.txt
  2. $ cat lsl.txt

We will see examples of sorting other fields, rather than sorting the default start characters.

7. Sort the file 'lsl.txt 'based on the second column (number of symbolic connections.

  1. $ sort -nk2 lsl.txt

Note: The '-n' parameter in the preceding example is used to sort the value content. To sort files based on the numeric column in the file, you must use the '-n' parameter.

8. Based on the 9th column (names of files and directories, non-numeric values sort the files 'lsl.txt.

  1. $ sort -k9 lsl.txt

9. The sort command can not only sort files, but also redirect the output content of the command to the sort command through pipelines.

  1. $ ls -l /home/$USER | sort -nk5

10. Sort the file tecmint.txt and delete duplicate rows. Check whether the duplicate rows have been deleted.

  1. $ cat tecmint.txt
  2. $ sort -u tecmint.txt

Currently, we have found the following sorting rules:

Unless the '-R' parameter is specified, the sorting priority is sorted according to the following rules.

  • The row that starts with a number has the highest priority.
  • Followed by the row priority starting with a lowercase letter
  • The content to be sorted is ordered alphabetically.
  • By default, the 'sort 'command performs the lexicographically ordered sorting of each line of keywords containing the sorted content as a string (the highest digit priority, see rule 1)

11. Create the third file 'lsla.txt 'in the frontend, and fill the content with the output content of the 'LS-la' command.

  1. $ ls -lA /home/$USER >/home/$USER/Desktop/tecmint/lsla.txt
  2. $ cat lsla.txt

Readers who know the ls command know that 'LS-la' is equal to 'LS-l' + hidden files. Therefore, most of these two files share the same content.

12. sort and output the content of the above two files.

  1. $ sort lsl.txt lsla.txt

Note that the file and directory are duplicated.

13. Now let's look at how to sort, merge, and delete duplicate rows for the two files.

  1. $ sort -u lsl.txt lsla.txt

At this point, we noticed that the duplicate rows have been deleted, and we can redirect the output content to the file.

14. We can also sort the file content based on multiple columns. Sort the output of the 'LS-l' Command Based on the 2, 5 (numeric) and 9 (non-numeric) columns.

  1. $ ls -l /home/$USER | sort -t ","-nk2,5-k9

So far, we will learn more detailed examples of the 'sort 'command in the following articles. So stay tuned. Maintain the spirit of sharing. If you like this article, please share it with your friends.

This article permanently updates the link address:

Related Article

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.