Sort
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: Sort by month name, such as JAN, DEC and so on;
-N: Sort using "pure number" (by default it is sorted by text type);
-R: Reverse sort;
-U: Is Uniq, in the same data, only one row is represented;
-T: delimiter, the default is to use the [tab] key to separate;
K: 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
A
Apache:x:48:48:apache:/var/www:/sbin/nologin
B
Bin:x:1:1:bin:/bin:/sbin/nologin
D
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
R
Root:x:0:0:root:/root:/bin/bash
U
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
O
Operator:x:11:0:operator:/root:/sbin/nologin
B
Bin:x:1:1:bin:/bin:/sbin/nologin
G
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
R
Root:x:0:0:root:/root:/bin/bash
D
Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
B
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
N
Nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
N
Ntp:x:106:113::/home/ntp:/bin/false
M
Messagebus:x:105:109::/var/run/dbus:/bin/false
S
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
P
Proxy:x:13:13:proxy:/bin:/bin/sh
B
Bin:x:2:2:bin:/bin:/bin/sh
S
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
R
Root:x:0:0:root:/root:/bin/bash
S
Syslog:x:101:102::/home/syslog:/bin/false
D
Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
Sync:x:4:65534:sync:/bin:/bin/sync
S
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: Counting
-U: only display unique rows
The contents of Testfile are as follows
Cat Testfile
H
Hello
W
World
F
Friend
H
Hello
W
World
H
Hello
Deleting an unordered file directly will reveal that no rows have been deleted
#uniq testfile
H
Hello
W
World
F
Friend
H
Hello
W
World
H
Hello
Sort files, the default is to go to heavy
#cat Words | Sort |uniq
Friend
H
Hello
W
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
F
Friend
Cut
The Cut command extracts text columns from a text file or text stream.
Cut grammar
[[email protected] ~]# cut-d ' delimited characters '-f fields <== used to have a specific delimiter character
[
[[email protected] ~]# cut-c character range <== used to arrange neat information
Options and Parameters:
-D: followed by delimited characters. Used with-F;
-F: Divides a piece of information into several paragraphs according to the delimiter character of-D, and uses-F to remove the meaning of the paragraph;
-C: Remove the fixed character range in units 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.
#echo $PATH | Cut-d ': '-f 1-3
/bin:/usr/bin:/sbin:
Take the path variable out, I want to find the first to third, there is a fifth path.
echo $PATH | Cut-d ': '-f 1-3,5
/bin:/usr/bin:/sbin:/usr/local/bin
Practical Example: Display only/etc/passwd users and shells
#cat/etc/passwd | Cut-d ': '-f 1,7
R
Root:/bin/bash
D
Daemon:/bin/sh
B
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: Only the travel is listed;
-W: Lists only the number of 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 #统计行数, which is commonly used when recording numbers
40/ETC/PASSWD #表示系统有40个账户
#
#wc-W/etc/passwd #统计单词出现次数
45/etc/passwd
#
#wc-M/etc/passwd #统计文件的字节数
1719
Seq
It has three options
-F,--format=format use printf style floating-point format (default:%G)
-S,--separator=string use STRING to separate numbers (default: \ n)
-W,--equal-width equalize width by padding with leading zeroes
-F is most commonly used, such as making 10 names dir001, dir002. DIR010 's catalogue, we can
Run the following command to
Seq-f ' dir%03g ' 1 10 | Xargs mkdir
Or
mkdir $ (seq-f ' dir%03g ' 1 10)
It is in printf format,%03g ' stands for three-bit floating-point numbers, in this way, such as with BASH3 printf
Also available as an equivalent command
printf ' dir%03d\n ' {1..10} | Xargs mkdir or mkdir ' printf ' dir%03d ' {1..10} '
Awk can of course also
awk ' BEGIN {while (num <) printf "dir%03d\n", ++num; exit} ' | Xargs mkdir
It would be quicker than writing a script, not written
For DIR in 001 002 003 004 005 006 007 008 009 010
Do
mkdir dir$
Done
You can also use SEQ to download a number of JPEG, as long as the format has a numerical order, especially in some XXX site;)
For i in ' seq-f '%02g ' 1 20 '
Do
if! Wget-p $HOME/tmp-c; Then
Wget-p $HOME/tmp-c $_
Fi
Done
The-s option mainly changes the output of the separator, the default is \ n, is newline
Case
With-S can be changed, such as
Seq-s ' 1 10
1 2 3 4 5 6 7 8 9 10 with a space as a splitter
Statistics Command processing time:
Time for I in $ (seq 1111);d o count=${#chars};d one;
..............................
The LSORT,UNIQ,CUT,WC,SEQ command in the shell uses