• grep与正则的搭配使用


    正则表达式

    一、什么是正则

    使用一些特殊符号+字母和数字按照某个规则组合成一个公式用来表示某个意思这就叫正则表达式

    二、正则用在哪些地方

    查找的场景特别适合使用正则
    vim 、grep、sed、awk等
    
    • 1
    • 2

    三、为什么需要使用正则

    四、正则和shell里面的区别

    1、shell里面:

    shell里的通配符: [] {} ^ $ !
    shell里面的?代表通配一个任意字符
    shell里面的*代表任意个任意字符

    2、正则里面:

    元字符:有特殊作用的字符,能描述其他字符的字符
    元字符:
    ^ 表示以什么开头
    $ 表示以什么结尾
    ? 表示前面的字符串出现0或者1次
    +表示前面的字符串出现1次以上
    *表示前面的字符串出现0次或者任意次

    [root@localhost lianxi]# rm -rf wangyutao*
    
    [root@localhost lianxi]# echo wangyuuuutaoooo|egrep "wangyu*"
    wangyuuuutaoooo
    [root@localhost lianxi]# echo wangyuuuuuuuuuuuuuuuuuuuuuuuuuuuutaoooo|egrep "wangyu*"
    wangyuuuuuuuuuuuuuuuuuuuuuuuuuuuutaoooo
    [root@localhost lianxi]# echo wangytaoooo|egrep "wangyu*"
    wangytaoooo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    五、基本正则和扩展正则的区别

    1、egrep = grep -E

    [root@localhost lianxi]# cat name.txt |grep -E -i  "wenkeke$|fangfang$"
    wenke  wenkeke
    wanglianfang fangfang
    wenke  wenkeke
    wanglianfang fangfang
    [root@localhost lianxi]# cat name.txt |egrep -i  "wenkeke$|fangfang$"
    wenke  wenkeke
    wanglianfang fangfang
    wenke  wenkeke
    wanglianfang fangfang
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、基本正则:
    元字符: * . ^ $

    [root@localhost lianxi]# cat name.txt |egrep  "^xiaomi"
    xiaomi  huawei  oppo
    xiaomi  huawei  oppo
    [root@localhost lianxi]# cat name.txt |egrep  "^xiaomi|lili$"
    xiaomi  huawei  oppo
    liyili lili
    xiaomi  huawei  oppo
    liyili lili
    # 匹配空行:
    [root@localhost lianxi]# cat name.txt |egrep  "^$"
    #显示行号:
    [root@localhost lianxi]# cat name.txt |egrep -n  "shenjiemi"
    16:shenjiemi
    17:shenjiemi lijunlin
    [root@localhost lianxi]# cat name.txt |egrep -n  "^shenjiemi$"  shenjiemi单独是一行
    16:shenjiemi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3、扩展正则:
    元字符: | + ? {}

    [root@localhost lianxi]# cat name.txt
    zhengyang   ZHENGYANG
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    wenke  wenkeke
    wanglianfang fangfang
    liyili lili
    zhengyang   ZHENGYANG
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    wenke  wenkeke
    wanglianfang fangfang
    liyili lili
    zhaojunjie
    shenjiemi
    shenjiemi lijunlin
    1
    2
    3
    4
    shenjiedami shenxiaomi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    扩展正则的使用

    [root@localhost lianxi]# echo "shenjiedami shenxiaomi" >>name.txt ^C
    [root@localhost lianxi]# cat name.txt | egrep "shen...."
    shenjiemi
    shenjiemi lijunlin
    shenjiedami shenxiaomi
    [root@localhost lianxi]# cat name.txt | egrep "shen.*"  表示shen字符串后面出现任意个任意字符
    shenjiemi
    shenjiemi lijunlin
    shenjiedami shenxiaomi
    [root@localhost lianxi]# cat name.txt | egrep "shen.{4}"
    shenjiemi
    shenjiemi lijunlin
    shenjiedami shenxiaomi
    [root@localhost lianxi]# cat name.txt | egrep "shen.{4,6}"  表示shen字符串后面出现4到6个任意字符
    shenjiemi
    shenjiemi lijunlin
    shenjiedami shenxiaomi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4、区别:
    扩展正则支持更加多的元字符,能够表示更加复杂的意思,功能更加强大
    5、[abc] 取中括号里的一个字符,可以是a也可以是b,也可以是c
    等同于"a|b|c"
    6、单词的定界符号
    < 和 \b 表示单词以什么开头
    > 和 \b 表示单词以什么结尾

    \<san  单词以san开头
    \<sanchuang\> 等同于 \bsanchuang\b
    \<sanchuang  等同于\bsanchuang
    sanchuang\>  等同于 sanchuang\b
    
    • 1
    • 2
    • 3
    • 4

    7、查找文本里单词的长度是13个字符的字符串:
    grep “\b[a-Z]{13}”

    grep命令

    一、grep的选项:

    grep选项
    -i 不区分大小写 -i, --ignore-case
    -o 只是显示匹配的内容 only-match
    -n 显示行号 line-number
    -v invert-match 取反
    -A after 在什么之后
    -B before 在什么以前
    -C center(中心) context上下文

    [root@localhost lianxi]# grep  -o  -i  "xiaomi"  name.txt
    xiaomi
    XIAOMI
    xiaomi
    [root@localhost lianxi]# grep  -o  -i  "xiaomi"  name.txt | wc -l
    3
    [root@localhost lianxi]#
    root@localhost lianxi]# cat name.txt |grep -B2 "lizhuofu"
    zhengyang   ZHENGYANG
    LIZHUOFU   lizhuofu123  12345lizhuofu
    --
    liyili lili
    zhengyang   ZHENGYANG
    LIZHUOFU   lizhuofu123  12345lizhuofu
    [root@localhost lianxi]# cat name.txt |grep -A2 "lizhuofu"
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    --
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    [root@localhost lianxi]# cat name.txt |grep -A 2 "lizhuofu"
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    --
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    [root@localhost lianxi]# cat name.txt |grep -C 2 "lizhuofu"
    zhengyang   ZHENGYANG
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    --
    liyili lili
    zhengyang   ZHENGYANG
    LIZHUOFU   lizhuofu123  12345lizhuofu
    xiaomi  huawei  oppo
    XIAOMI xiaomi12234+56  sc@163.com
    [root@localhost lianxi]#
    [root@localhost lianxi]# grep -r  "xiaomi"  /lianxi
    /lianxi/name.txt:xiaomi  huawei  oppo
    /lianxi/name.txt:XIAOMI xiaomi12234+56  sc@163.com
    /lianxi/name.txt:xiaomi  huawei  oppo
    /lianxi/name.txt:XIAOMI xiaomi12234+56  sc@163.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    sort和uniq命令

    一、sort是一个排序命令

    默认情况下根据一行里的第1个字母的ASCII码值进行比较,升序排列
    如果第1个字符一样,再比较第2个,以此类推
    1、选项:

    -n  进行数值的比较,默认升序
    -k  指定列排序
    -r  排序是降序
    -t, --field-separator=SEP  指定分隔符
       	field 字段,列
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、sort的分隔符是空白(空格和tab),多个空白都算一个分隔符
    在这里插入图片描述
    在这里插入图片描述

    二、uniq是一个去重命令

    默认情况下,只能去除连续的重复行
    1.选项:-c 统计重复的次数 count
    在这里插入图片描述
    在这里插入图片描述

    今日练习

    1.进入/lianxi目录,复制/etc/passwd到当前目录下,然后对passwd进行操作
    2、查找出当前passwd文件中以ftp或者mail开头的行,在屏幕上输出。
    3、查找出当前passwd文件中首行不是以r、m、f开头的行,在屏幕上输出。
    4、查找出当前passwd文件中以bash结尾的行。
    5、查找出/etc/login.defs文件中的有效行(不显示空行和注释行)。
    6、查找出/var/log/messages文档中有16个字母的单词
    7、查找出来/etc/passwd文件里用户名包含liu同时使用bash的用户
    8、查找/etc/ssh/sshd_config 里的有效行
    9、查找出/etc/ssh/sshd_config 文件里的包含连续2个数字的行
    10、查找出/etc/ssh/sshd_config包含特殊字符的行
    11、查找出/etc/ssh/sshd_config不包含数字的行
    14、写一个表示下面网址的正则表达式出来。例如:
    http://www.baidu.com fjdkfjdkfj
    http://www.sina.com  fengdeyong
    http://www.163.com
    http://www.12306.cn  1212121
    http://www.qillu.edu  sanchuang
    rsync://www.github.com/abc
    ftp://192.168.0.1
    ftp://www.baidu.com  12112
    15.时间的正则
    	时间的正则,表示18/Dec/2021:16:54分钟到18/Dec/2021:16:58分钟
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    答案

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    SCADA系统在石油炼制行业的应用:如何实现石油炼制过程实时数据采集与监控?
    etcd之读性能主要影响因素
    Bom浏览器对象模型
    【Leetcode】【每日一题】【中等】1465. 切割后面积最大的蛋糕
    20241028软考架构-------软考案例8答案
    一文详解Redis企业版软件!
    揭开 Amazon Bedrock 的神秘面纱 | 基础篇
    SAP 标准产品里使用 OData 服务的一些细节分享
    Pytest自动化框架运行全局配置文件pytest.ini
    Mac 下 Go 的安装和卸载
  • 原文地址:https://blog.csdn.net/weixin_47661174/article/details/125565479