In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use the sort command in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use the sort command in Linux.
The Linux common command sort command sorts the input lines by key value field and data type option as well as locale.
Sort sorts all lines in the text file.
Summary sort [OPTION]... [FILE]... Sort [OPTION]... -- the main purpose of files0-from=F is to sort and output the contents of all input files.
Read standard input when there is no file or when the file is -.
Options sort options:
-b,-- ignore-leading-blanks ignores the opening whitespace. -d,-- dictionary-order considers only white space, letters, and numbers. -f,-- ignore-case considers lowercase letters as uppercase letters. -g,-- general-numeric-sort is sorted by number. -I,-- ignore-nonprinting excludes unprintable characters. -M,-- month-sort is sorted in non-month, January, and December order. -h,-- human-numeric-sort sorts by storage capacity (note the use of uppercase letters, for example: 2K 1G). -n,-- numeric-sort is sorted by number. -R,-- random-sort are sorted randomly, but grouped into the same rows. -- random-source=FILE gets bytes of random length from FILE. -r,-- reverse sorts the results in reverse order. -- sort=WORD is sorted according to WORD, where: general-numeric is equivalent to-g Magi Humanism numeric, where-h general-numeric is equivalent to-M Magi numeric is equivalent to-n journal random equivalent to-R ~ ~ version is equivalent to-V. -V,-- the natural ordering of numbers in version-sort text. Other options:
-- batch-size=NMERGE merges up to NMERGE inputs at a time; temporary files are used for the excess. -c,-- check,-- check=diagnose-first checks whether the input is sorted, which does not perform sorting. -C,-- check=quiet,-- check=silent is similar to the-c option, but does not output the first unsorted row. -- compress-program=PROG uses PROG to compress temporary files; PROG-d to extract them. -- debug comments the lines used for sorting and sends alerts for suspicious usage to stderr. -- files0-from=F reads all file names that end with NUL from file F; if F is -, read names from standard input. -k,-- key=KEYDEF is sorted by a key; KEYDEF gives the location and type. -m,-- merge merges sorted files, and then no longer sorts. -o,-- output=FILE writes the result to FILE instead of standard output. -s,-- stable stabilizes sorting by disabling the final comparison. -S,-- buffer-size=SIZE uses SIZE as the memory cache size. -t,-- field-separator=SEP uses SEP as the column delimiter. -T,-- temporary-directory=DIR uses DIR as the temporary directory instead of $TMPDIR or / tmp; using this option multiple times to specify multiple temporary directories. -- parallel=N changes the number of sorts that run concurrently to N. -u,-- unique use-c at the same time, strictly check the sort; do not use-c at the same time, output the results of sorting and de-duplicating. -z,-- zero-terminated sets the line Terminator to NUL (empty), not the newline character. -- help displays help information and exits. -- version displays version information and exits. The format of KEYDEF is: F [.C] [OPTS] [, F [.C] [OPTS]], indicating the position from start to end. F represents the column number C indicating that the OPTS is one or more characters in [bdfgiMhnRrV] to override the current sorting option. Use the-- debug option to diagnose incorrect usage. SIZE can have the following multiplication suffixes: 1% of memory; b 1; K 1024 (default); the remaining M, G, T, P, E, Z, Y can be inferred. Parameter FILE (optional): any number of files to be processed.
A return value of 0 indicates success, while a non-zero value indicates failure.
The example sort compares each line of the file / text as a unit by comparing the values of the ASCII code from the first character to the back, and then outputting them in ascending order.
Root@ [mail text] # cat sort.txt aaa:10:1.1 ccc:30:3.3 ddd:40:4.4 bbb:20:2.2 eee:50:5.5 eee:50:5.5 [root@mail text] # sort sort.txt aaa:10:1.1 bbb:20:2.2 ccc:30:3.3 ddd:40:4.4 eee:50:5.5 eee:50:5.5 ignores the use of the-u option or uniq:
[root@mail text] # cat sort.txt aaa:10:1.1 ccc:30:3.3 ddd:40:4.4 bbb:20:2.2 eee:50:5.5 eee:50:5.5 [root@mail text] # sort-u sort.txt aaa:10:1.1 bbb:20:2.2 ccc:30:3.3 ddd:40:4.4 eee:50:5.5 [root@mail text] # uniq sort.txt aaa:10:1. 1 ccc:30:3.3 ddd:40:4.4 bbb:20:2.2 eee:50:5.5sort-n, Use of-r,-k,-t options:
[root@mail text] # cat sort.txt AAA:BB:CC aaa:30:1.6 ccc:50:3.3 ddd:20:4.2 bbb:10:2.5 eee:40:5.4 eee:60:5.1 # arrange the BB columns in numerical order from smallest to largest: [root@mail text] # sort-nk 2-t: sort.txt AAA:BB:CC bbb:10:2.5 ddd:20:4.2 aaa:30:1. 6 eee:40:5.4 ccc:50:3.3 eee:60:5.1 # sorts the CC column numbers from largest to smallest: #-n is sorted by number size -r is in reverse order,-k is the field to be sorted, and-t specifies the field separator as the colon [root@mail text] # sort-nrk 3-t: sort.txt eee:40:5.4 eee:60:5.1 ddd:20:4.2 ccc:50:3.3 bbb:10:2.5 aaa:30:1.6 AAA:BB:CC 's interpretation and example of the-k option:
-k option in-depth interpretation:
FStart.CStart Modifier,FEnd.CEnd Modifier-Start-,-End- FStart.CStart option, FEnd.CEnd option this syntax format can be divided into two parts, the Start section and the End section. The Start section consists of three parts, of which the Modifier part is the options section we talked about earlier; let's focus on the FStart and C.Startscape C. start of the Start section that can be omitted, which means starting at the beginning of the field. FStart.CStart, where FStart is the field used, and CStart represents the first character of the sort from which character in the FStart field. Similarly, in the End section, you can set FEnd.CEnd. If you omit .Cend or set it to 0, it means the last character ending in the field.
Example: sort from the second letter of the company's English name:
$sort-t'- k 1.2 facebook.txt baidu 5000 sohu 4500 google 5000 guge 50 3000 interpretation:-k 1.2 is used to sort strings from the second character of the first field to the last character of the field. You will find that baidu tops the list because the second letter is a. Both sohu and google have the second character o, but the h of sohu precedes the o of google, so they are ranked second and third, respectively. Guge is only in fourth place.
Example: sort only by the second letter of the company's English name, if the same sort is in descending order by employee's salary:
$sort-t'- k 1.2 facebook.txt baidu 3 facebook.txt baidu 5000 google 5000 sohu 4500 guge 50 3000 interpretation: since only the second letter is sorted, we use the representation of-k 1.2, which means that we only sort the second letter (if you ask me why not to use-k 1.2)? Of course not, because you omitted the End section, which means you will sort the strings from the second letter to the last character in the field. To sort the wages of employees, we also use-k 3prime3, which is the most accurate expression, which means that we only sort this field, because if you omit the following 3, we sort the content from the beginning of the third field to the last domain.
Note the difference between the-g and-n options: stackoverflow
For learning about this complex command, it is recommended that you read the info documentation and refer to blogs, Q & A sites, etc.
This command is a command in the GNU coreutils package. For help, please see man-s 1 shuf,info coreutils' shuf invocation'.
The above is all the contents of the article "how to use sort commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.