Shell special symbols cut command, sort, wc, uniq command, tee, tr, split command

shell special symbols _cut command

概念:cut命令用来截取某一个字段
格式:cut -d '分割字符' [-cf] n,这里的n是数字,该命令选项有如下几个:
- d 后面跟分割字符,分割字符要用单引号括起来
- c 后面接的是第几个字符
- f 后面接的是第几个区块

The cut command usage is as follows

[root@localhost do]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost do]# cat /etc/passwd |head -2 |cut -d ':' -f 1
root
bin
[root@localhost do]# cat /etc/passwd |head -2 |cut -d ':' -f 1,2
root:x
bin:x
[root@localhost do]# cat /etc/passwd |head -2 |cut -d ':' -f 1-3
root:x:0
bin:x:1

cat passwd this file, head only looks at the first two lines, cut -d intercepts the segmentation symbol as ":", -f 1 means intercepts the first paragraph, 1,2 means the first two paragraphs, 1-3 means the first three paragraphs.

sort sort_wc count lines_uniq delete duplicate lines

Introduction to the sort command

sort命令用于排序
格式:sort [-t 分隔符] [-kn1,n2] [-nru]
这里-n1和n2指的是数字,其他选项如下:
-t 后面跟分割字符,作用跟cut -d选项一样,截取符号是什么;
-n 表示使用纯数字排序,字母及特殊符号表示为0;
-r 表示反向排序;
-u 表示除去重复;
-kn1,n2 表示由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序。
  • For example: if sort does not add any options, it is output in ascending order by default. We assume to see what is the difference between the first five lines of the passwd configuration file.

    [root @localhost /]# head -n5 /etc/passwd |sort 
    #View the first five lines of passwd, output to sort (sort) execution, and sort in ascending order by default.
    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

Example 2: Write the first 10 lines of the passwd configuration file to 3.txt, and add a few special symbols and numbers for sorting.

2018.4.21 The fourth class in five weeks (shell special symbols, commands such as cut interception)
As shown in the figure, we use sort to sort, sort by numbers -n
[root@localhost do]# sort -n 3.txt #-n means positive order, special symbols and letters are expressed as 0
2018.4.21 The fourth class in five weeks (shell special symbols, commands such as cut interception)
as shown in the first picture Show, let's -r for reverse sorting
[root@localhost do]# sort -r 3.txt #-r means reverse order
2018.4.21 The fourth class in five weeks (shell special symbols, commands such as cut interception)

Introduction to the wc command for statistics

概念:wc命令用于统计文档的行数,字符数或词数。
选项:
-l 统计行数
-m 统计字符数
-w 统计词数,以空格作为区分
  • The specific operations are as follows:

    [root@localhost do]# wc /etc/passwd #View the number of lines, words and words of this document
    19 27 846 /etc/passwd
    [root@localhost do]# wc -l /etc/passwd #Count the number of lines
    19 /etc/passwd
    [root@localhost do]# wc -m /etc/passwd #Count word count
    846 /etc/passwd
    [root@localhost do]# wc -w /etc/passwd #Count line count
    27 /etc/passwd

Command uniq to remove duplicate lines

概念:uniq命令用来删除重复的行,改名了只有-c选项比较常用;
它表示统计重复的行数,并把行数写在前面。
  • Write a file like the following:

    [root@localhost do]# touch 4.txt #Create a text
    [root@localhost do]# vi 4.txt #Edit content
    111
    222
    333
    111

  • Before using uniq, it must be dedicated to file sorting, otherwise it will not work. The example is as follows:

    [root@localhost do]# cat 4.txt #View the content inside
    111
    222
    333
    111
    [root@localhost do]# uniq 4.txt #Test to see if it works without sorting, if it doesn’t work
    111
    222
    333
    111
    [root @localhost do]# sort 4.txt |uniq #sort sort 4.txt file, and then output to uniq to delete duplicate lines
    111
    222
    333
    [root@localhost do]# sort 4.txt |uniq -c #Count duplicate lines , how many
    2 111
    1 222
    1 333

Command tee, redirect and display content

tee命令后面跟文件名,起作用类似于重定向>,但它比重定向多一个功能;
把echo输出的结果,通过管道符“|” tee输出给后面的文件并在屏幕上显示。
  • The specific operations are as follows:

    [root@localhost do]# touch 1.txt #Create a text
    [root@localhost do]# echo "1321asd123" | tee 1.txt #Pass the content of echo through the pipe symbol and output it to the tee command to the 1.txt file , and the result is displayed on the screen.
    1321asd123

Command tr, for replacing characters

tr命令用于替换字符,常用来处理文档中出现的特殊符号;
如DOS文档中出现的符号^M,该命令常用的选项如下:
-d 表示删除某个字符,后面跟要删除的字符;
-s 表示删除重复的字符。
  • Suppose the small l of a file is changed to a large L

    [root@localhost do]# echo "linux" > 1.txt 
    [root@localhost do]# cat 1.txt 
    linux
    [root@localhost do]# echo "linux" | tr '[l]' '[L]' #Change the small l in front to L
    Linux

command split, to cut the document

split命令用于切割文档,常用的选项为-b和-l
-b 表示依据大小来分割文档,默认单位为byte(字节)
-l 表示依据行数来分割文档
  • First search out the content, and cat all the content out, append and redirect to a file

    [root@localhost /]# find /etc/ -type f -name "*conf" -exec cat {} >> /tmp/do/1.txt \;
    #find search the file name under /etc/ and ask for the end of conf file, the content viewed by cat {} is appended to the 1.txt file.
    [root@localhost /]# du -sh /tmp/do/1.txt #View the size of this file
    212K /tmp/do/1.txt

  • Example 1: This file has a size of 212K. Let's try cutting it with a specified size of 100K. The example is as follows:

    2018.4.21 The fourth class in five weeks (shell special symbols, commands such as cut interception)
    #Here, -b specifies to bring down 100K, and it is displayed in bytes by default without a unit.

  • Example 2: Specify a file whose target filename starts with 123.

    2018.4.21 The fourth class in five weeks (shell special symbols, commands such as cut interception)
    The file starting with #x is the result of Example 1. You don't need to look at him. Look at the file starting with 123 to form a comparison.

shell special symbols

重点章节,以后会经常用到

special symbol $

  • The symbol $ can be used as an identifier in front of a variable, and can also be used with ! Used in combination, examples are as follows:

[root@localhost do]# 
[root@localhost do]# ls /tmp/do/
123aa 1.txt 2.txt 3.txt 4.txt a.txt xaa xab xac
[root@localhost do]# !ls
ls /tmp/do/
123aa 1.txt 2.txt 3.txt 4.txt a.txt xaa xab xac
!$表示上条命令中的最后一个变量。

special symbols;

  • If you want to run two or more commands on a line, you need to put symbols between the commands; an example is:

    [root@localhost dior1]# mkdir 123 ; touch 1.txt ; touch 2.txt ; ls
    123 1.txt 2.txt
    As above, a directory is created, 2 files are created, and finally ls is checked and executed

special symbol ~

  • The symbol ~ represents the user's home directory, the root user's home directory is /root, and the ordinary user's home directory is /home/username

special symbol &

  • If you want to put a command into the background for execution, you need to add the symbol &, which is usually used when the command runs for a long time, and can be used in sleep (sleep), for example:

    [root@localhost ~]# sleep 30 & 
    [1] 40966
    [root@localhost ~]# jobs 
    [1]+ 运行中 sleep 30 &

Usage of redirection symbols > , >> , 2> , 2>> , &>

概念:>、>>他们分别代表取代(>)和追加(>>)的意思;
当我们运行一个命令报错时,报错信息会输出到当前屏幕;
如果想重定向到一个文本,则需要用重定向符号2>或2>>;
他们分别表示错误重定向和错误追加重定向。
&>表示错误和正确的重定向输入到一个文件里去

Brackets []

  • The brackets are character combinations, which represent any one of the character combinations. (mentioned in the previous chapter)

Special symbols && and ||

  • When using ||, it means or, which means that if two commands are separated by ||, after the first command is executed successfully, the second command will not be executed, and if the first command is an error, it will be executed if the execution fails. Article 2.

    Example 1, assuming both commands are correct
    [root@localhost do]# ls 1.txt || wc -l 2.txt 
    1.txt

Example 2: Assuming the first command is wrong and the second command is correct, the second one will be executed
[root@localhost do]# ls 111111.txt || wc -l 2.txt #here at all there is no string of txt files of 1
ls: cannot access 111111.txt: no such file or directory
0 2.txt

  • Use && to indicate that the following command will be executed only after the previous command is executed successfully. If the previous command is unsuccessfully executed, the latter command will not be executed. Use && to separate, to judge.

    Example 1, when both commands are correct 
    [root@localhost do]# ls 1.txt && wc -l 2.txt #ok, where both commands are valid
    1.txt
    0 2.txt

Example 2, if the first command is unsuccessful, the following commands will not be executed.
[root@localhost do]# ls 111.txt && wc -l 2.txt 
ls: cannot access 111.txt: No such file or directory

Introduction to common special symbols

 * 任意个任意字符
 ? 任意一个字符
 #注释字符
 \ 脱义字符
 | 管道符

Guess you like

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