8.10 shell special symbols cut, sort,wc,uniq, tee,tr,split commands

Keywords: Linux shell Anaconda ascii

shell special symbol cut command

  • cut partition, - d separator - f specifies segment number - c specifies the number of characters
  • Sort sort, - n in numeric order - r reverse order - t separator - kn1/-kn1,n2
  • Wc-l Statistical Lines-m Statistical Characters-w Statistical Words
  • uniq de-weighting, - c statistical rows
  • tee is similar to > and redirects are displayed on the screen at the same time
  • TR replaces characters, tr'a''b', case replaces tr' [a-z]''[A-Z]'
  • split cut, - b size (default unit byte), - l line number

cut command

  • The cut command is used to intercept a string
  • Format: cut - d "splitter" file name
  • - d: Following the split character, the split character is enclosed in double quotation marks.
  • - f: Following the string
  • -c: followed by a few characters
  1. Interception of paragraphs 1 to 3
[root@linux-128 ~]# head -5 1.txt |cut -d ":" -f 1-3
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4

  1. Interception of paragraphs 1 and 3
[root@linux-128 ~]# head -5 1.txt |cut -d ":" -f 1,3
root:0
bin:1
daemon:2
adm:3
lp:4

  1. Intercept the second character
[root@linux-128 ~]# head -5 1.txt |cut -c 2
o
i
a
d
p

  1. Intercept the second character to the fifth character
[root@linux-128 ~]# head -5 1.txt |cut -c 2-5
oot:
in:x
aemo
dm:x
p:x:

sort command

  • The sort command is used for sorting. The format sort [-t splitter] [-kn1, n2] [-nur], n1,n2 refers to numbers.
  • -t separator
  • -k is sorted by columns; interval ranges are comma-k3,5
  • -n sorted by number
  • - r in reverse order, sorted from typing to minor
  • -u to repeat
  • The beginning of the - un letter will be recognized as duplicate content, such as skj1 a weotj will be considered duplicate content, only display digital content.
  1. sort does not add any options, from the first line character backward, in turn by ASCII code value comparison, sorted by ascending order
[root@linux-128 ~]# vim 2.txt
111
111
111
111aaa
121
!@#$@123
1asd
abab
abab
adm:x:3:4:adm:/var/adm:/sbin/nologin
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
root:x:0:0:root:/root:/bin/bash

  1. Sort by number
[root@linux-128 ~]# sort -n 2.txt
!@#$@123
abab
abab
adm:x:3:4:adm:/var/adm:/sbin/nologin
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
root:x:0:0:root:/root:/bin/bash
1asd
111
111
111
111aaa
121

  • Note: If there are letters or special symbols, they are treated as 0 by default in numeric sorting.
  1. Reverse sorting
[root@linux-128 ~]# sort -nr 2.txt
121
111aaa
111
111
111
1asd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
abab
abab
!@#$@123

  1. To repeat, look at the letters here as zero, and then repeat.
[root@linux-128 ~]# sort -nu 2.txt
abab
1asd
111
121

wc command

  • The wc command is used to count the number of lines, characters and words in a document.
  • - l Statistical Row Number
  • - m Statistical Character Number
  • - w Statistics Word Number
  1. View file 3.txt rows
[root@linux-128 ~]# cat 3.txt
abab
1asd
111
121
[root@linux-128 ~]# wc -l 3.txt
4   3.txt

  1. See how many characters there are in the file 3.txt, $is the end-of-line character
[root@linux-128 ~]# cat -A 3.txt
abab$
1asd$
111$
121$
[root@linux-128 ~]# wc -m 3.txt
18 3.txt

  1. - w statistical word, which is divided by spaces or blank characters.
[root@linux-128 ~]# cat 3.txt
abab
1asd
111
121
[root@linux-128 ~]# wc -w 3.txt
4 3.txt

  1. If wc is followed by no options and directly followed by the document, the number of lines, words and characters will be output in turn.
[root@linux-128 ~]# wc 3.txt
 4  4 18 3.txt

uniq command

  • The uniq command is used to delete duplicate rows, usually in conjunction with sort.
  • The - c option is commonly used to count the number of duplicated rows and to write the number of rows in front
[root@linux-128 ~]# sort 2.txt
111
111
111
111aaa
121
!@#$@123
1asd
abab
abab
adm:x:3:4:adm:/var/adm:/sbin/nologin
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
root:x:0:0:root:/root:/bin/bash

  1. Sort first and then delete duplicate rows
[root@linux-128 ~]# sort 2.txt |uniq -c
      3 111
      1 111aaa
      1 121
      1 !@#$@123
      1 1asd
      2 abab
      1 adm:x:3:4:adm:/var/adm:/sbin/nologin
      1 bin:x:1:1:bin:/bin:/sbin/nologin
      1 daemon:x:2:2:daemon:/sbin:/sbin/nologin
      1 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
      1 root:x:0:0:root:/root:/bin/bash

[root@linux-128 ~]# sort 2.txt |uniq
111
111aaa
121
!@#$@123
1asd
abab
adm:x:3:4:adm:/var/adm:/sbin/nologin
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
root:x:0:0:root:/root:/bin/bash

tee command

  • The file name after the tee command acts like redirection >, but it has one more function on the screen than redirection.
[root@linux-128 ~]# cat 3.txt >4.txt
[root@linux-128 ~]# cat 3.txt |tee 4.txt
abab
1asd
111
121

  1. tee-a is equivalent to chase-and-add orientation; it has an additional function to display on the screen.
[root@linux-128 ~]# cat 3.txt |tee -a 4.txt
abab
1asd
111
121
[root@linux-128 ~]# cat 3.txt |tee -a 4.txt
abab
1asd
111
121
[root@linux-128 ~]# cat 4.txt
abab
1asd
111
121
abab
1asd
111
121
abab
1asd
111
121

Command tr

  • The tr command is used to replace characters
[root@linux-128 ~]# cat 3.txt |tr 'a' 'A'
AbAb
1Asd
111
121

[root@linux-128 ~]# cat 3.txt |tr '[a-z]' '[A-Z]'
ABAB
1ASD
111
121

split command

  • Split is used to split documents
  • Split-l is divided by rows
  • Split-b is divided by size
  1. split -b
[root@linux-128 ~]# mkdir test
[root@linux-128 ~]# cd test
[root@linux-128 test]# find /etc/ -type f -name "*.conf" -exec cat {} >1.txt \;
[root@linux-128 test]# du -sh 1.txt
252K	1.txt
[root@linux-128 test]# split -b 50k 1.txt
[root@linux-128 test]# ls
1.txt  xaa  xab  xac  xad  xae  xaf

  1. split –l
[root@linux-128 test]# wc -l 1.txt
6479 1.txt
[root@linux-128 test]# split -l 1000 1.txt
[root@linux-128 test]# ls
1.txt  xaa  xab  xac  xad  xae  xaf  xag

  • If the file name is not specified after split, it will be xaa, xab.... Such a file name to access the cut file
  1. Specify the target partition file name abc.
[root@linux-128 test]# split -l 1000 1.txt abc.
[root@linux-128 test]# ls
1.txt  abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  abc.ag

Special symbols

  • Variable prefix,!$combination, which means end of line
  • Multiple commands are written on one line, separated by semicolons
  • 6550
  • Place the command behind it and leave it behind the scenes.
  • Redirection > pursuit aggravation orientation > error redirection 2 > error pursuit aggravation orientation 2 > correct and error redirection & >
  • [] One of the specified characters, [0-9],[a-zA-Z],[abc]
  • | | and &&, between commands
  • $: Can be used as an indicator in front of a variable, and! Used in combination, the end of the line is represented in the regular
[root@linux-128 test]# ls /root
11.txt      123     1.txt   2.txt        3.txt   anaconda-ks.cfg  d6z
11.txt.bak  1a.txt  +2      321.txt      4.txt   a.txt            test
12          1.log   23.txt  321.txt.bak  ab.txt  b.txt            Job number.txt
[root@linux-128 test]# ls !$
ls /root
11.txt      123     1.txt   2.txt        3.txt   anaconda-ks.cfg  d6z
11.txt.bak  1a.txt  +2      321.txt      4.txt   a.txt            test
12          1.log   23.txt  321.txt.bak  ab.txt  b.txt            Job number.txt

  • Multiple commands are written on one line and used; partitioned.
[root@linux-128 ~]# cat 2.txt;cat 3.txt
111
111aaa
121
111
abab
1asd
abab
!@#$@123
111
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
abab
1asd
111
121

  • ~ Home directory, followed by regular matchers

  • Redirection (correct)> overrides previous content

[root@linux-128 ~]# echo "121212">3.txt
[root@linux-128 ~]# cat 3.txt
121212
  • Pursuit and Acceleration Orientation (Correct)>
[root@linux-128 ~]# echo "ababab">>3.txt
[root@linux-128 ~]# cat 3.txt
121212
Ababab
  • Error redirection 2 >
[root@linux-128 ~]# cat 5.txt 2>6.txt
[root@linux-128 ~]# cat 6.txt
cat: 5.txt: No file or directory

  • Mistake pursuit aggravation orientation 2 >
[root@linux-128 ~]# cat 6.txt
cat: 5.txt: No file or directory
cat: 5.txt: No file or directory

  • Errors and corrections are redirected to a file.
[root@linux-128 ~]# cat 3.txt 5.txt &> 6.txt
[root@linux-128 ~]# cat 6.txt
121212
ababab
cat: 5.txt: No file or directory

  • Link symbols in shell & & & |;
  • & & the following commands will not be executed until the previous commands have been executed successfully; if the previous commands have not been executed successfully, the latter commands will not be executed.
  • The previous command is executed unsuccessfully before the latter command is executed; if the former command is executed successfully, the latter command is not executed.
  • If the command on the left is successful or not, the command on the back will be executed.

Posted by lmaster on Tue, 08 Jan 2019 18:33:10 -0800