The sort command sorts the rows in the files specified by the file parameter and writes the results to standard output. If the file parameter specifies multiple files, the sort command connects the files and sorts them as a file.
Sort syntax
[[email protected] ~]# sort [-fbmnrtuk] [file or stdin] options and Parameters:-F: Ignores case differences, such as a and a are treated as encodings;-B: Ignores the first whitespace part;-M: In the name of the month Word to sort, such as the sort method of JAN, DEC and so on;-N: Sort by using "pure number" (default is sort by text type);-r: Reverse sort;-U: Uniq, the same data, only one line is represented;-T: delimiter, the default is to use the [tab] key to divide N: The meaning of sorting by that interval (field)
Sort the accounts of the/etc/passwd
[Email protected] ~]# CAT/ETC/PASSWD | Sort Adm:x:3:4:adm:/var/adm:/sbin/nologin Apache:x:48:48:apache:/var/www:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/ Nologin Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Sort is sorted by default with the first data, and by default it is sorted by string, so the letter a begins in ascending order.
/ETC/PASSWD content is separated by:, I want to sort by the third column, How to
[Email protected] ~]# CAT/ETC/PASSWD | Sort-t ': '-K 3 root:x:0:0:root:/root:/bin/bash Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin Operator:x:11:0:o Perator:/root:/sbin/nologin Bin:x:1:1:bin:/bin:/sbin/nologin Games:x:12:100:games:/usr/games:/sbin/nologin
The default is to sort by string, if you want to use numbers to sort:
cat/etc/passwd | Sort-t ': '-K 3n root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh
The default is ascending sort, if you want to sort in reverse order, as follows
cat/etc/passwd | Sort-t ': '-K 3nr nobody:x:65534:65534:nobody:/nonexistent:/bin/sh Ntp:x:106:113::/home/ntp:/bin/false messagebus:x : 105:109::/var/run/dbus:/bin/false Sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
If you want to/etc/passwd, first sort by the 2nd character of the sixth field to the 4th character, and then reverse-sort based on the first field.
cat/etc/passwd | Sort-t ': '-K 6.2,6.4-k 1r sync:x:4:65534:sync:/bin:/bin/sync proxy:x:13:13:proxy:/bin:/bin/sh bin:x:2:2:bin:/bin:/ Bin/sh sys:x:3:3:sys:/dev:/bin/sh
See how many shells the/etc/passwd has: sort the seventh field of/etc/passwd and then go back to the heavy one:
cat/etc/passwd | Sort-t ': '-K 7-u root:x:0:0:root:/root:/bin/bash Syslog:x:101:102::/home/syslog:/bin/false daemon:x:1:1:daemon:/usr /sbin:/bin/sh Sync:x:4:65534:sync:/bin:/bin/sync Sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
Uniq
The Uniq command removes duplicate rows from a sorted file, so Uniq is often used in combination with sort. That is, in order for the Uniq to work, all duplicate rows must be contiguous.
Uniq syntax
[[email protected] ~]# uniq [-ICU] options and Parameters:-I: Ignoring the difference between uppercase and lowercase characters;-C: Count-u: Only show unique rows
The contents of Testfile are as follows
Cat testfile Hello World friend Hello World Hello
Deleting an unordered file directly will reveal that no rows have been deleted
#uniq testfile Hello World friend Hello World Hello
Sort files, the default is to go to heavy
#cat Words | Sort |uniq friend Hello World
Duplicate rows are deleted after sorting, and the number of occurrences of the row is output at the beginning of the line
#sort Testfile | Uniq-c 1 friend 3 Hello 2 world
Displays only duplicate rows, and displays the number of repetitions of the row at the beginning of the line
#sort Testfile | UNIQ-DC 3 Hello 2 World
Show only rows that are not duplicates
Sort Testfile | Uniq-u Friend
Cut
The Cut command extracts text columns from a text file or text stream.
Cut grammar
[[email protected] ~]# cut-d ' The delimited character '-f fields <== used to have a specific delimiter character [[email protected] ~]# cut-c character range <== used for neatly arranged letters Options and Parameters:-D: followed by delimited characters. Use with-F;-F: Divides a piece of information into several segments according to the delimiter character of-D, and uses-F to remove the meaning of the paragraph;-C: Remove the fixed character interval in the unit of character (characters);
The PATH variable is as follows
[Email protected] ~]# echo $PATH/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/games # 1 | 2 | 3 | 4 | 5 | 6 | 7
To take the path variable out, I'm going to find a fifth path.
#echo $PATH | Cut-d ': '-f 5/usr/local/bin
Take the path variable out and I'll find the third and fifth paths.
#echo $PATH | Cut-d ': '-f 3,5/sbin:/usr/local/bin
Take the path variable out and I'll find the third to last path.
echo $PATH | Cut-d ': '-f 3-
/sbin:/usr/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/games
Take the path variable out and I'll find the first to third path.
# $PATH | -d-f-
/bin:/usr/bin:/sbin:
Take the path variable out, I want to find the first to third, there is a fifth path.
$PATH | -d-f-,
/bin:/usr/bin:/sbin:/usr/local/bin
Practical Example: Display only/etc/passwd users and shells
#cat/etc/passwd | Cut-d ': '-f 1,7 root:/bin/bash daemon:/bin/sh bin:/bin/sh
WC
The number of words in the statistics file, how many lines, how many characters.
WC syntax
[[email protected] ~]# WC [-LWM] options and Parameters:-L: Row only;-W: Lists only how many words (English word);-M: how many characters;
Use WC statistic/etc/passwd by default
#wc/etc/passwd 1719/etc/passwd
40 is the number of rows, 45 is the number of words, and 1719 is the number of bytes
The WC commands are relatively simple to use and each parameter is used as follows:
#wc-L/etc/passwd #统计行数, 40/etc/passwd #表示系统有40个账户 #wc-w/etc/passwd #统计单词出现次数 45/etc/passwd #wc-M when the number of records is used /etc/passwd #统计文件的字节数 1719
Linux SORT,UNIQ,CUT,WC Command detailed