zoukankan      html  css  js  c++  java
  • grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq

    grep:正则表达式,文本过滤工具,能够实现以指定的"模式(Pattern)"逐行搜索文件中的内容,并将匹配到的行显示出来.

    模式:是由正则表达式的元字符,其他字符组合起来的匹配字符。

    每一类正则表达式本身的表达式是需要用户自己去写的,但表达式的元字符都有着固定的或者特定的意义,我们可以根据自己的需要去理解或者组合字符,生成我们需要的模式

     -v:显示不被模式匹配到的行,invert-match  

    -i:在做模式匹配的时候不区分大小写ignore-case   

    -o:只显示匹配到的串,而非默认显示匹配到的行,only-matching

    -A 数字:A=after,显示到匹配到的行后,还显示每一个匹配项下面*行的内容  

    -B 数字:before,前*

    -C 数字:context,上下文*

    -E:扩展的正则表达式

    例子:

     [root@lbg test]# grep -o 'root' /etc/passwd      --只显示字段,不显示整行。

    [root@lbg test]# grep  -A 1 'root' /etc/passwd --显示该行和下一行内容

    [root@lbg test]# grep  -B 2 'root' /etc/passwd  --显示该行和其前两行内容。


    1.字符匹配( . [] [^] [:space:] [:punct:])

      . 匹配任意单个字符(制表符,空格,标点,字母,数 字,其他符号)

    [] 匹配指定范围内的任意单个字符

    [^] 指定范围外的单个字符

    [:space:] 匹配空白字符,包括空格,tab,

    [:punct:] 标点符号(用法同空白字符)

    例子:

    [root@lbg test]# grep 'r[oa]t' /etc/passwd
    operator:x:11:0:operator:/root:/sbin/nologin
    sshd:x:74:74:Privilege-separated  SSH:/var/empty/sshd:/sbin/nologin
    2.次数匹配
     * (贪婪模式) 做次数匹配,匹配*前面的字符0,1或多次,只表示次数,不表示字符

    只做次数匹配,01次,写法是:  ?

    {m,n} 最少m,最多n次    写法为:   {m,n}

    用{m,n}可以没有上限,但下限一定要有.  ?{}都要加.

    例子:
     [root@lbg test]# cat 1
    ab
    a b
    a  b
    a    b
    [root@lbg test]# grep 'a b' 1  --匹配1个空格
    a b
    [root@lbg test]# grep 'a *b' 1  --匹配0及以上的空格
    ab
    a b
    a  b
    a   b
    a    b
    [root@lbg test]# grep 'a ?b' 1 --匹配0或1个空格
    ab
    a b
    [root@lbg test]# grep 'a {2,3}b' 1 ---匹配2或3个空格
    a  b
    a   b
    [root@lbg test]# grep 'a {2,}b' 1  --匹配2个及以上空格
    a  b
    a   b
    a    b
    3.锚定符<>^$
     <                      锚定词首       ####找到以r..t开头的内容  

    >         锚定词尾          ###   r..t>

    脱字符     行首锚定

    $         行尾锚定        ##### root$       -->必须以root结尾

    例子:
     
    [root@lbg test]# grep '
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
     
    [root@lbg test]# grep 'ot>' /etc/passwd   ---ot结尾的单词
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin

    [root@lbg test]# grep '^ro' /etc/passwd  --以ro开头的行
    root:x:0:0:root:/root:/bin/bash

    [root@lbg test]# grep 'bash$' /etc/passwd  --以bash结尾的行
    4.大括号的使用:在复制较长路径的文件并重命名时可使用大括号
     
     [root@lbg test]# ls                      ---无文件
    [root@lbg test]# touch a             --创建文件a
    [root@lbg test]# cp /test/a{,1}     --将a文件复制,并在其原路径下重命名为a1.
    [root@lbg test]# ls
    a  a1
     
    5.wc命令:统计作用。(word count)
     
     wc [options]...  file_name

    -l 仅显示行数(line)     -w  仅显示单词数(word)        -c  仅显示字节数(char)

    单独的wc显示的信息依次是:行数 单词数 字节数  文件名

    例子:
    [root@lbg test]# cat a
    how do you do
    how do you do
    [root@lbg test]# wc a        ---显示4种信息
     2  8 28 a
    [root@lbg test]# wc -l a    --显示行数
    2 a
    [root@lbg test]# ls /test 
    a  b  c  d
    [root@lbg test]# ls /test | wc -l      ----使用wc统计目录下文件的个数
    4
    6.tr命令:转换显示的结果。
     
     tr:转换字符或者删除字符(tr -d '字符集合'   --delete 

    tr '集合1'  '集合2'       --将集合1里的内容按集合2对应位置的内容替换显示出来。

    例子:

     [root@lbg test]# echo 'abcdabcd' |tr 'ac' '13'     --两边集合一一对应的情况
    1b3d1b3d
    [root@lbg test]# echo 'abcdabcd' |tr 'acd' '13'    --集合1多于集合2的情况
    1b331b33
    [root@lbg test]# echo 'abcdabcd' |tr 'acd' '1234' --集合1少于集合2的情况
    1b231b23
    [root@lbg test]# echo 'abcdabcd' |tr 'a-z' 'A-Z'   --大小写转换
    ABCDABCD
    [root@lbg test]# echo 'abcdabcd' |tr -d 'ab'      ---删除字符
    cdcd
    7.cut:剪切
     -c :按照字符数量切 (characters)

    -d :按指定分隔符切(delimiter 定界符)

    -f:指定要显示的字段  (file)

    单个数字-->一个字段

    逗号分隔的多个数字-->指定多个离散字段

    -  -->连续字段,3-5表示35字段

    例子:
     [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2       --按空格切
    are
    [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2,4  --第2个和第4个
    are dogs
    [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2-4  --2到4个
    are many dogs
    [root@lbg test]# echo 'there are many dogs'|cut -d m -f  1  --以m切割
    there are 
    [root@lbg test]# echo 'there are many dogs'|cut -d m -f  2  --以m切割
    any dogs
    [root@lbg test]# echo 'how do you do'|cut -c 2-5   ---取第2到第5个字符
    ow d
    8.sort排序:sort [options] file ...
       -f:忽略字符大小写(ignore-case)

     -n:对数值进行排序 //Sort 默认字符排序  加,-n 数字排序.

        -r : 逆序输出

     -t:指定分隔符

     -k:基于哪个字段进行排序  (key)

     -u:uniq,重复的行只显示一行 (unique)

    例子:
     [root@lbg test]# cut -d : -f 3 /etc/passwd |sort -n
    [root@lbg test]# cat a
    234
    123
    234
    [root@lbg test]# sort -u a
    123
    234
    [root@lbg test]# sort -n  a
    123
    234
    234
    [root@lbg test]# sort -n  -u a
    123
    234

    9.uniq:去重复行。

       -c:统计每一行出现的次数(count)

     -d:仅显示重复过的行

     -u:仅显示未重复行

    注意uniq去重,认定是连续的才是重复的.不是连续的不会去重.

    例子:
     
     [root@lbg test]# cat a
    234
    234
    123
    234
    [root@lbg test]# uniq a
    234
    123
    234
    [root@lbg test]# sort a | uniq -c
      1     123
      3     234
    [root@lbg test]# sort -u -n a
    123
    234
    10.小括号向前引用功能

    1--引用从左到右的第一个括号内范围.

    2--引用从左到右的第二个括号内范围.

    例子:

    [root@lbg test]# cat a
    he love his lover
    she like her liker
    [root@lbg test]# grep 'l..e.*e' a
    he love his lover
    she like her liker
    [root@lbg test]# grep '(l..e).*1' a  ---与命令grep 'l..e.*e' a相同
    he love his lover
    she like her liker
    [root@lbg test]# grep '(l(..)e).*1' a  ---与命令grep 'l..e.*e' a相同
    he love his lover
    she like her liker
    [root@lbg test]# grep 'l..e.*l..'   a          
    he love his lover
    she like her liker
    [root@lbg test]# grep '(l(..)e).*2' a   --与grep 'l..e.*l..'   a 相同
    he love his lover
    she like her liker

  • 相关阅读:
    一场由空格引发的错误数据
    ECS服务器搭建Discuz 邮箱设置,报错处理
    MS SQL CASE WHEN 的用法
    sql prompt 缩写 快捷键
    CentOS 防火墙
    docker swarm
    docker machine
    docker-compose 配置
    docker-compsoe & .netcore & nginx
    dockerfile
  • 原文地址:https://www.cnblogs.com/lbg-database/p/10108363.html
Copyright © 2011-2022 走看看