cut sort wc uniq tee tr split command
• cut division, -d delimiter -f specifies the segment number -c specifies the number of characters
cut -d ":" -f 1 /etc/passwd Intercept the first segment of the /etc/passwd file separated by:
cut -d ":" -f 2,4 /etc/paaswd Intercept the first segment in the /etc/passwd file separated by:
cut -d ":" -f 1-3 /etc/passwd Intercept 1 to 3 segments in the /etc/passwd file separated by:
head -n 3 /etc/passwd |cut -d":" -f 1 The first three lines of /etc/passwd screenshot the first paragraph
cut -c 6 /etc/passwd extracts the sixth character of each line
cut -c 1-8 /etc/passwd extract the first eight characters of each line
• sort sort, -n sort numerically -r reverse order -u deduplicate -t delimiter -kn1/-kn1,n2
sort /etc/passwd sort does not add any options, then from the first character backward, compare by ASCII code value in turn,
Finally output them in ascending order.
head -n 5 /etc/passwd |sort -t: -k3 -n
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-t is followed by a separator, -k is followed by a number, which indicates the string ordering of the first region, and -n indicates that pure numeric ordering is used.
head -n5 /etc/passwd | sort -t: -k3,5 -r
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
-k3,5 indicates the string ordering from the 3rd to the 5th area, -r indicates reverse ordering.
If you use -nu together, sort will treat the letters in the document as 0
[root@localhost ~]# cut -d: -f1 /etc/passwd |sort -n -u
root
Only the first line is left, because it treats all strings as 0
• wc -l count lines -m count characters -w count words -c display Bytes size -L display the longest line
[root@localhost ~]# cat file
123
abc
[root@localhost ~]# cat -A file
123$
abc$
[root@localhost ~]# wc -l file
2 file
[root@localhost ~]# wc -c file
8 file
[root@localhost ~]# wc -m file
8 file
[root@localhost ~]# wc -w file
2 file
When -m counts characters, the newline $ at the end of each line is also counted
-w statistical words are calculated with spaces as delimiters
• uniq to deduplicate, -c to count lines
The premise of uniq deduplication is to sort first. If the same lines are not together, it will not take effect.
Common formats:
sort file |uniq
sort file |uniq -c
[root@localhost ~]# cat 123
12
away
12
a
[root@localhost ~]# uniq 123
12
away
12
a
[root@localhost ~]# sort 123 |uniq
12
a
away
[root@localhost ~]# sort 123 |uniq -c
2 12
1 a
1 off
[root@localhost ~]#
• tee is similar to >, redirects are displayed on screen; -a appends option
[root@localhost ~]# sort 123 |uniq -c |tee a.txt
2 12
1 a
1 off
[root@localhost ~]# cat a.txt
2 12
1 a
1 off
[root@localhost ~]# sort 123 |uniq -c |tee -a a.txt
2 12
1 a
1 off
[root@localhost ~]# cat a.txt
2 12
1 a
1 off
2 12
1 a
1 off
[root@localhost ~]#
• tr replaces characters, tr 'a' 'b', case replaces tr '[az]' '[AZ]'
[root@localhost ~]# echo "gavin" |tr 'g' 'G'
Gavin
• split split, -b size (default unit bytes), -l lines
split -b 100 file to split by 100 bytes
split -b 10k file to cut by 10k size
split -b 1M file to cut by 1M size
split -l 100 file to split every 100 lines
split -l 100 file f specifies that the name of the split file starts with f
split -a specifies the number of letters used to form the suffix part of the output name file
split -d replaces letter suffixes with numbers
[root@localhost gavin]# ll -h file
-rw-r - r--. 1 root 13M 6 月 30 20:04 file
[root@localhost gavin]# split -b 1M file f
[root@localhost gavin]# ls
faa fab fac fad fae faf fag fah fai faj fak fal fam file
[root@localhost gavin]# split -l 1000 file file_
[root@localhost gavin]# ls
file file_aa file_ab file_ac file_ad file_ae file_af
Special symbols:
• * any number of any characters
• ? any character
• # comment character
• \ escape character
• | pipe character
• $ variable prefix, !$ combination, which means the end of the line
• ; multiple commands are written on one line, separated by semicolons
• ~ User home directory, followed by a regular expression to indicate a match
• & after the command will drop the command to the background
• > >> 2> 2>> &>
• [ ] specifies one of the characters, [0-9],[a-zA-Z],[abc]
• || and &&, used between commands
|| Or if the previous command is successfully executed, the second command is not executed; if the previous command is unsuccessful, the latter command is executed;
&& and the previous command is executed successfully, the latter command will be executed, otherwise the latter command will not be executed
; No matter whether the previous command is executed successfully or not, the latter command will be executed;
[root@localhost gavin]# [ -d gavin ] || mkdir gavin
[root@localhost gavin]# ls
gavin
[ -d gavin ] Check if there is a gavin directory, if not, create gavin;
[root@localhost gavin]# [ -d gavin ] && mkdir gavin1
[root@localhost gavin]# ls
gavin gavin1
[ -d gavin ] Check if there is a gavin directory, and sometimes create gavin1;