2018.4.21 Five week fourth class (Shell special symbol, cut intercept and other commands)

Source: Internet
Author: User
Tags sorts

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

[Email protected] do]# cat/etc/passwd |head-2
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
[[email protected] do]# cat/etc/passwd |head-2 |cut-d ': '-F 1
Root
Bin
[Email protected] do]# cat/etc/passwd |head-2 |cut-d ': '-f
Root:x
Bin:x
[[email protected] do]# cat/etc/passwd |head-2 |cut-d ': '-f 1-3
root:x:0
Bin:x:1

Cat passwd This file, head only look at the first two lines, cut-d intercept the split symbol as ":",-F 1 to intercept the first paragraph, the previous two paragraphs, 1-3 represents the first three paragraphs.
Sort sorts _WC count rows _uniq Delete duplicate rows Sort command Introduction
sort命令用于排序格式:sort [-t 分隔符] [-kn1,n2] [-nru]这里-n1和n2指的是数字,其他选项如下:-t 后面跟分割字符,作用跟cut -d选项一样,截取符号是什么;-n 表示使用纯数字排序,字母及特殊符号表示为0;-r 表示反向排序;-u 表示除去重复;-kn1,n2 表示由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序。
    • Example: If sort does not have any options, he is the default ascending output, and we assume that there is a difference between the five elements before looking at the passwd configuration file.

      [Email protected]/]# head-n5/etc/passwd |sort
      #查看passwd前五行, output to sort (sort) execution, sorted by default ascending order.
      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 then add a few special symbol numbers to sort.


, we sort by sorts, sort by numbers-n
[[email protected] do]# sort-n 3.txt #-n indicates a positive sequence, and special symbols and letters are represented as 0

As shown in the first diagram, we-R gives the reverse sort look
[[email protected] do]# sort-r 3.txt #-r for reverse order

WC command Introduction, used to count
概念:wc命令用于统计文档的行数,字符数或词数。选项:-l 统计行数-m 统计字符数-w 统计词数,以空格作为区分
    • Here's how:

      [Email protected] do]# wc/etc/passwd #查看这个文档的行数, number of words and word count
      846/etc/passwd
      [Email protected] do]# wc-l/etc/passwd #统计行数
      19/etc/passwd
      [Email protected] do]# wc-m/etc/passwd #统计字数
      846/etc/passwd
      [Email protected] do]# wc-w/etc/passwd #统计行数
      27/etc/passwd

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

      [email protected] do]# Touch 4.txt #创建一个文本
      [Email protected] do]# VI 4.txt #编辑内容
      111
      222
      333
      111

    • Before using Uniq, it must be dedicated to file sorting, otherwise, the example is as follows:

      [email protected] do]# cat 4.txt #查看里面的内容
      111
      222
      333
      111
      [Email protected] do]# uniq 4.txt #测试一下看看不排序管用么, no matter
      111
      222
      333
      111
      [[email protected] do]# sort 4.txt |uniq #sort排序4. txt file, then output to uniq delete duplicate rows
      111
      222
      333
      [Email protected] do]# sort 4.txt |uniq-c #统计重复的有行, how many
      2 111
      1 222
      1 333

Command tee, redirect and display content
tee命令后面跟文件名,起作用类似于重定向>,但它比重定向多一个功能;把echo输出的结果,通过管道符“|” tee输出给后面的文件并在屏幕上显示。
    • Here's how:

      [email protected] do]# Touch 1.txt #创建一个文本
      [Email protected] do]# echo "1321asd123" | Tee 1.txt #把echo的内容通过管道符号, output to the tee command, to a 1.txt file, and display the results on the screen.
      1321asd123

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

      [[email protected] do]# echo "Linux" > 1.txt
      [email protected] do]# cat 1.txt
      Linux
      [[email protected] do]# echo "Linux" | TR ' [l] ' [l] ' #把前面的小l, changed to L
      Linux

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

      [Email protected]/]# find/etc/-type f-name "*conf"-exec Cat {} >>/tmp/do/1.txt \;
      #find search for the file name under/etc/Ask Conf the end of the file, cat {} The contents of the view, appended to the 1.txt file.
      [Email protected]/]# Du-sh/tmp/do/1.txt #查看这个文件的大小
      212k/tmp/do/1.txt

    • Example 1: This file has 212K, we give him a cut specify a size of 100K to try, the example is as follows:


      #这里,-B is specified with 100K, and no units are displayed in bytes by default.

    • Example 2: Specify the file that begins with the target file name 123.


      #x开头的文件, which is the result of example 1, does not need to look at him, look at the 123-beginning file to form a contrast.

Shell Special Symbols
  key chapters, which will often be used later in the "# # # # # Special symbol $-symbol $ can be used as the identifier before the variable, and can be! Used together, examples are as follows: >[[email protected] do]# [[email protected] do]# ls/tmp/do/123aa 1.txt 2.txt 3.txt 4.txt A. TXT XAA xab xac[[email protected] do]#!lsls/tmp/do/123aa 1.txt 2.txt 3.txt 4.txt a.txt xaa xab xac!$ = Previous Bar The last variable in the command. # # # Special symbols;-If you want to run two or more commands in a row, you need to add a symbol between the commands: >[[email protected] dior1]# mkdir 123; Touch 1.txt; Touch 2.txt; ls123 1.txt 2.txt above meaning, created a directory, and created 2 files, and the last LS View execution # # # special symbol ~-symbol ~ Indicates the user's home directory, the root user's home directory is/root, the ordinary user is/home/username#### Special symbol &-If you want to put a command in the background, you need to add a symbol &, which is usually used for long command times, can be used in sleep (sleep), the example is as follows: >[[email protected] ~]# sleep  & [1] 40966[[email protected] ~]# jobs [1]+ run in Sleep &#### redirect symbols >, >>, 2>, 2>>, &> usage  

Concept:>, >> they represent the meaning of substitution (>) and append (>>) respectively;
When we run a command error, the error message will be output to the current screen;
If you want to redirect to a text, you need to use the redirect symbol 2> or 2>>;
They represent error redirection and error append redirection, respectively.
&> indicate errors and correct redirection input into a file

#### 中括号[] - 中括号内为字符组合,代表字符组合中的任意一个。(上一章节有提到)#### 特殊符号 &&和||- 使用||时,表示或者,意思是说 如果两条命令用||分割的话,第一条执行成功后,第二条就不会执行,假如第一条命令是错误的话,执行失败就会执行第二条。> 示例1,假设两个命令都是正确的> [[email protected] do]# ls 1.txt || wc -l 2.txt 1.txt>示例2: 假设第一条命令是错误的,而第二条命令是正确的,就会执行第二条[[email protected] do]# ls 111111.txt || wc -l 2.txt  #在这里 根本就没有一串1的txt文件ls: 无法访问111111.txt: 没有那个文件或目录0 2.txt- 使用&& 表示前面的命令执行成功以后,才会执行后面的命令,如果前面命令执行不成功,后面的命令就不会执行。用&&分割,用来判断的。>示例1,两条命令都是正确的情况下 >[[email protected] do]# ls 1.txt && wc -l 2.txt #ok,在这里两条命令都生效1.txt0 2.txt>示例2,假如第一条命令不成功,后面的命令就不会执行。>[[email protected] do]# ls 111.txt && wc -l 2.txt ls: 无法访问111.txt: 没有那个文件或目录# 普通特殊符号介绍
    • Any of any characters
      ? Any one character
      #注释字符
      \ de-Semantic characters
      | Pipe character

2018.4.21 Five week fourth class (Shell special symbol, cut intercept and other commands)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.