In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to talk to you about how the 'sort' command of Linux is used. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
What is sort?
Sort is a Linux program used to sort the contents of single or multiple text files.
The Sort command sorts files with spaces as field delimiters and splits a line into multiple keywords.
Note that unless you redirect the output to a file, the Sort command does not actually sort the contents of the file (that is, the contents of the file are not modified), but outputs the contents of the file in order.
Actual combat demonstration
1. Target file creation
First create a text file (tecmint.txt) to execute the 'sort' command.
The working path is'/ home/$USER/Desktop/tecmint'.
The'- e' option in the following command enables'\ 'escape and resolves'\ n' to line feeds:
2. View the file
Before we start learning the 'sort' command, let's take a look at the contents of the file and how it is displayed:
3. Basic sorting
Now, sort the contents of the file using the following command.
Note: the above command does not actually sort the contents of the file, but simply outputs its contents in an orderly manner.
4. Sort and output
Sort the contents of the file 'tecmint.txt' file, and output the sorted contents to a file named sorted.txt, and then use the cat command to view the contents of the validated sorted.txt file.
5. Arrange in reverse order
Now use the'- r 'parameter to reverse the contents of the' tecmint.txt' file, redirect the output to the 'reversesorted.txt' file, and use the cat command to view the contents of the file.
6. Wear and build new documents
Create a new file (lsl.txt) that contains the output of the'ls-l 'command executed in the home directory.
We will see examples of sorting other fields instead of the default start character.
7. Sort based on the number of symbolic connections in a column
Sort the file 'lsl.txt' based on the second column (the number of symbolic connections).
Note: the'- n 'parameter in the above example indicates that the numeric content is sorted. You must use the'- n 'parameter when you want to sort files based on numeric columns in the file.
8. Sort based on a column name
Sort the file 'lsl.txt' based on column 9 (file and directory names, not numeric values).
9. Output content redirection
The sort command can not only sort files, but we can also redirect the output of the command to the sort command through the pipeline.
10. Sort and de-duplicate
Sort the file tecmint.txt and delete the duplicate lines. Then check to see if the duplicate row has been deleted.
Collation:
Unless the'- r 'parameter is specified, the priority of the sort is sorted according to the following rules
Line priority that begins with a number *
Lines that begin with lowercase letters take second place.
The contents to be sorted are sorted in dictionary order.
By default, the 'sort' command lexicographically sorts each line of keywords with sorted content as a string (numeric priority *, see rule 1)
11. Output content
Create a third file, 'lsla.txt',' at the current location, and its contents are populated with the output of the'ls-lA' command.
Readers who know the ls command know that'ls-lA' 'equals' ls-l'+ hidden files, so most of the contents of the two files are the same.
12. Sort output of two files
Sort the contents of the above two files.
13. Two files are sorted, merged and deduplicated
Now let's see how to sort, merge, and delete duplicate lines of two files.
At this point, we notice that the duplicate lines have been deleted, and we can redirect the output to the file.
14. Sort based on multiple columns
We can also sort the contents of the file based on multiple columns. Sorts the output of the'ls-l 'command based on columns 2, 5 (numeric) and 9 (non-numeric).
After reading the above, do you have any further understanding of how Linux's' sort' command is used? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.