Shell special symbols cut command, sort_wc_uniq command, tee_tr_split command, under shell special symbols

Extension:

1. The difference between source exec  http://alsww.blog.51cto.com/2001924/1113112
2. A complete collection of Linux special symbols http://ask.apelearn.com/question/7720
3. Sort is not sorted by ASCII  http:/ /blog.csdn.net/zenghui08/article/details/7938975

shell special symbol cut command

  • cut split, -d delimiter -f specifies the segment number -c specifies the number of characters
  • sort sort, -n sort numerically -r reverse order -t separator -kn1/-kn1,n2
  • wc -l count lines -m count characters -w count words
  • uniq deduplication, -c counts the number of lines
  • tee is similar to >, the redirect is also displayed on the screen
  • tr replacement character, tr 'a' 'b', case replacement tr '[az]' '[AZ]'
  • split split, -b size (default unit bytes), -l number of lines

cut command

  • The cut command is used to cut a string
  • Format: cut -d "delimiter" filename
  • -d: followed by a split character, the split character is enclosed in double quotes;
  • -f: followed by the number of paragraphs of the string
  • -c: followed by the first few characters
  1. Intercept passwd paragraphs 1 to 3
[root@yong-02 test]# head -3 passwd |cut -d ":" -f 1-3
root:x:0
bin:x:1
daemon:x:2
  1. Intercept passwd paragraphs 1 and 3
[root@yong-02 test]# head -3 passwd |cut -d ":" -f 1,3
root:0
bin:1
daemon:2
  1. Truncate the 3rd character
[root@yong-02 test]# head -3 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@yong-02 test]# head -3 passwd |cut -c 3
o
n
e
  1. Truncate the 2nd to the 6th character
[root@yong-02 test]# head -3 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@yong-02 test]# head -3 passwd |cut -c 2-6
oot:x
in:x:
aemon

sort command

  • The sort command is used for sorting, the format sort [-t separator] [-kn1, n2] [-nur], n1, n2 refer to numbers
  • -t delimiter
  • -k Sort by column; use commas for interval range -k3,5
  • -n sort numerically
  • -r sort in reverse order, sorted from hit to small
  • -u deduplicate
  • -un The letters starting with the letter will be recognized as repeated content, such as skj1 a weotj will be considered as repeated content, only the digital content will be displayed
  1. sort does not add any options, from the first line of characters backwards, compares by ASCII code value in turn, and sorts in ascending order
[root@yong-02 test]# vim passwd 
adf
123
111
!!!
!jk
lfas
<::
>,
%^
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
  1. Sort by numbers
[root@yong-02 test]# sort -n passwd 
<::
>,
!!!
%^
adf
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
!jk
lfas
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
111
123
  • Note: If there are letters or special symbols, they are regarded as 0 by default in numerical sorting.
  1. reverse sort
[root@yong-02 test]# sort -nr passwd 
123
111
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
root:x:0:0:root:/root:/bin/bash
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
lfas
!jk
halt:x:7:0:halt:/sbin:/sbin/halt
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adf
%^
!!!
>,
<::
  1. To repeat, here the letters are regarded as 0, and then to repeat.
[root@yong-02 test]# sort -nu passwd 
adf
111
123

wc command

  • The wc command is used to count the number of lines, characters, and words in a document.
  • -l count lines
  • -m count characters
  • -w count words
  1. View the number of lines in file 1.txt
[root@yong-02 test]# cat 1.txt
abcd
1abcd
123
111
121
[root@yong-02 test]# wc -l 1.txt 
5 1.txt
  1. Check how many characters there are in file 1.txt, $ is the end of line character
[root@yong-02 test]# cat -A 1.txt
abcd$
1abcd$
123$
111$
121$
[root@yong-02 test]# wc -m 1.txt 
23 1.txt
  1. -w counts words, which are separated by spaces or whitespace characters.
[root@yong-02 test]# cat 1.txt
abcd
1abcd
123
111
121
[root@yong-02 test]# wc -w 1.txt 
5 1.txt
  1. If no option is added after wc, and the document is followed directly, the number of lines, words, and characters will be output in turn.
[root@yong-02 test]# cat 1.txt
abcd
1abcd
123
111
121
[root@yong-02 test]# wc 1.txt 
 5  5 23 1.txt

uniq command

  • The uniq command is used to remove duplicate lines, usually used in conjunction with sort
  • The -c option is more commonly used, it means to count the number of repeated lines and write the number of lines in front
[root@yong-02 test]# cat 2.txt 
111
222
123
111
222
adf
123
111
!!!
!jk
lfas
<::
>,
%^
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  1. Sort first then remove duplicate rows
[root@yong-02 test]# sort 2.txt |uniq -c
      1 <::
      1 >,
      1 !!!
      1 %^
      3 111
      2 123
      2 222
      1 adf
      1 bin:x:1:1:bin:/bin:/sbin/nologin
      1 daemon:x:2:2:daemon:/sbin:/sbin/nologin
      1 !jk
      1 lfas
      1 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
      1 root:x:0:0:root:/root:/bin/bash
      3 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

tee command

  • The file name after the tee command is similar to redirection>; but it has one more function displayed on the screen than redirection;
[root@yong-02 test]# cat 1.txt  > a.txt 
[root@yong-02 test]# cat 1.txt |tee a.txt 
abcd
1abcd
123
111
121
  1. tee -a is equivalent to appending redirection >>; it has an additional function of displaying on the screen
[root@yong-02 test]# cat 1.txt |tee -a a.txt 
abcd
1abcd
123
111
121
[root@yong-02 test]# cat a.txt 
abcd
1abcd
123
111
121
abcd
1abcd
123
111
121

command tr

  • tr command is used to replace characters
[root@yong-02 test]# cat 1.txt |tr 'a' 'A'
Abcd
1Abcd
123
111
121
[root@yong-02 test]# cat 1.txt |tr 'a-z' 'A-Z' ##最好'[a-z]' '[A-Z]'
ABCD
1ABCD
123
111
121

split command

  • split is used to split the document
  • split -l split by line
  • split -b split by size
  1. split -b split by size
[root@yong-02 test]# find /etc/ -type f -name "*.conf" -exec cat {} >test.txt \;
[root@yong-02 test]# du -sh test.txt 
240K	test.txt
[root@yong-02 test]# split -b 100k test.txt 
[root@yong-02 test]# ls
test.txt  xab   xaa   xac
  1. split -l split by line
[root@yong-02 test]# wc -l test.txt 
6082 test.txt
[root@yong-02 test]# split -l 1000 test.txt 
[root@yong-02 test]# ls
test.txt  xab  xad  xaf
xaa       xac  xae  xag
  • If no file name is specified after split, the split file will be accessed with file names such as xaa, xab...
  1. Specify the target split file name as abc.
[root@yong-02 test]# split -l 1000 test.txt abc.
[root@yong-02 test]# ls
 abc.ab  abc.ad  abc.af  test.txt
 abc.aa  abc.ac  abc.ae  abc.ag  

special symbols

  • $ 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 that represents a match
  • & put it after the command, it will drop the command to the background
  • Redirect > Append Redirect >> Error Redirect 2 > Error Append Redirect 2 >> Correct and Error Redirect &>
  • [ ] specifies one of the characters, [0-9],[a-zA-Z],[abc]
  • || and &&, used between commands
  • $: can be used as an identifier in front of a variable, and can be combined with! Used in combination to indicate the end of the line in the regular
[root@yong-02 test]# ls /tmp/gzip/
1.txt  2.txt.zip  4.txt  test  yyl.tar      yyl.tar.gz  yyl.zip
2.txt  3.txt      5.txt  yyl   yyl.tar.bz2  yyl.tar.xz
[root@yong-02 test]# ls !$
ls /tmp/gzip/
1.txt  2.txt.zip  4.txt  test  yyl.tar      yyl.tar.gz  yyl.zip
2.txt  3.txt      5.txt  yyl   yyl.tar.bz2  yyl.tar.xz
  • ; Multiple commands are written on one line, separated by ;
[root@yong-02 test]# cat 1.txt ;cat a.txt
abcd
1abcd
123
111
121
abcd
  • ~ home directory, followed by a regular expression match

  • Redirect (correct) > , which overwrites previous content

[root@yong-02 test]# echo "abcdefg" >a.txt
[root@yong-02 test]# cat a.txt 
abcdefg
  • Append redirect (correct) >>
[root@yong-02 test]# echo "1234" >>a.txt
[root@yong-02 test]# cat a.txt 
abcdefg
1234
  • Error redirect 2>
[root@yong-02 test]# cat c.txt 2>a.txt
[root@yong-02 test]# cat a.txt 
cat: c.txt: 没有那个文件或目录
  • error append redirect 2 >>
[root@yong-02 test]# cat c.txt 2>>a.txt
[root@yong-02 test]# cat a.txt 
cat: c.txt: 没有那个文件或目录
cat: c.txt: 没有那个文件或目录
  • &> Errors and corrects are redirected to a file.
[root@yong-02 test]# cat 1.txt c.txt &>a.txt
[root@yong-02 test]# cat a.txt
abcd
1abcd
123
111
121
cat: c.txt: 没有那个文件或目录
  • link symbol && || in shell;
  • && After the previous command is executed successfully, the following command will be executed; if the previous execution is unsuccessful, the latter command will not be executed

  • || If the execution of the previous command is unsuccessful, the latter command will be executed; if the execution of the former command is successful, the latter command will not be executed.

  • ; Whether the command on the left is successful or not, the following commands will be executed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325440589&siteId=291194637