• shell脚本——正则表达式



    一、基本概念

    正则表达式(Regular Expression、regex或regexp,缩写为RE),也译为正规表示法、常规表示法,是一种字符模式,用于在查找过程中匹配指定的字符。

    许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎,或者说Java的String也是支持正则表达式的

    正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。

    支持正则表达式的程序如:locate |find| vim| grep| sed |awk

    二、第一类正则

    1. 名词解释

    元字符:那些在则表达式中具有特殊意义的专用字符,比如:点(.)、星()、问号(?)等
    前导字符:即位于元字符前面的字符:abc
    、aooo.

    2. 正则中常用的元字符

    符号作用
    .匹配任意单个字符,除了换行符
    *前导字符出现0次或连续多次
    .*任意长度的字符
    ^字符串的开头
    $字符串的结尾
    ^$空字符串
    []匹配指定字符组中的任意单个字符
    [^]匹配不在指定字符组内的任意字符
    ^[]匹配值以指定字符组中的任意一个字符开头
    ^[^]匹配不以指定字符组中任意一个字符开头
    \{n\}匹配前导字符连续出现n次
    \{n,\}匹配前导字符至少出现n次
    \{n,m\}匹配前导字符出现n次与m次之间
    \<取单词开头
    \>取单词结尾
    \<\>精确匹配字符或字符串
    \(strings\)保存被匹配的字符

    3. 测试

    [root@localhost ~]# cat test.txt
    ggle
    gogle
    google
    gooogle
    goooooogle
    gooooooogle
    taobao.com
    taotaobaobao.com
    
    jingdong.com
    dingdingdongdong.com
    10.1.1.1
    Adfjd8789JHfdsdf/
    a87fdjfkdLKJK
    7kdjfd989KJK;
    bSKJjkksdjf878.
    cidufKJHJ6576,
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    # 包含.com
    [root@localhost ~]# grep '.com' test.txt
    
    # 以 go 开头
    [root@localhost ~]# grep '^go' test.txt
    
    # 以com结尾
    [root@localhost ~]# grep 'com$' test.txt
    
    # 匹配8和m字符
    [root@localhost ~]# grep '[8m]' test.txt
    
    # 不匹配 8 和 m 字符
    [root@localhost ~] grep '[^8m]' test.txt
    
    # 匹配以ht开头
    [root@localhost ~]# grep '^[ht]' test.txt
    
    # 匹配不以 g和t 开头
    [root@localhost ~]# grep '^[^gt]' test.txt
    
    # 精确匹配 taobao 这个字符串
    [root@localhost ~]# grep '\' test.txt
    
    # 匹配前导字符连续出现3个字符 o
    [root@localhost ~]# grep  'o\{3\}' test.txt
    
    # 匹配前导字符至少连续出现4个字符 o
    [root@localhost ~]# grep  'o\{4,\}' test.txt
    
    # 匹配前导字符o连续出现2次到4次之间
    [root@localhost ~]# grep  'o\{2,4\}' test.txt
    
    • 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

    vim底行模式

    在vim模式里(通过\(strings\)保持不变)
    将192.168.44.144 变成192.167.44.168

    • @#只是分割符(自定义的),效果一样
    • g:替换多次,只能替换一行
    • %s:针对整个文档中的所有关键词进行替换
    • \.:转义点符号
      基本格式:%s/要替换的字符/替换为什么字符

    \1就等价于 192.
    \2等价于 .44

    :%s#\(192\.\)168\(\.44\)\.144#\1167\2\.168
    :%s@\(192\)\.168\(\.44\)\.144@\1\.167\2\.168
    
    • 1
    • 2

    将光标处锁所在行10.1.1.1替换成10.1.200.200

    • \1:等价于10.1
    :s#\(10\.1\)\.1\.1#\1.200.200
    
    • 1

    将10.1.1.1变成10.200.200.200

    • g:替换多次
    • #号只是分割符号
    :%s#.1#.200#g
    
    • 1

    将helloworld yourself 换成hellolilei myself

    :%s#\(hello\)world your\(self\)#\1lilei my\2#g 
    
    • 1

    使用sed命令

    • -n:只显示匹配结果
    [root@localhost ~]# sed -n 's/\(hello\)world your\(self\)/\1lilei my\2/p' test.txt
    hellolilei myself
    
    • 1
    • 2

    4. Perl内置正则

    -P 可以让grep使用perl的正则表达式语法

    • \d:匹配数字 [0-9]
    • -w: 匹配字母数字下划线[a-zA-Z0-9_]
    • \s:匹配空格、制表符、换页符[\t\r\n]
    #grep -P '\d' test.txt
    #grep -P '\w' test.txt
    #grep -P '\s' test.txt
    
    • 1
    • 2
    • 3

    5.扩展正则

    使用 扩展类的正则表达式 grep -E 或则 egrep

    表达式作用
    +匹配一个或多个前导字符
    ?匹配零个或一个前导字符
    a|b匹配a或b
    ()组字符
    {n}前导字符重复n次
    {n,}前导字符重复至少n次
    {n,m}前导字符重复n到m次

    三、第二类正则

    表达式功能
    [:alnum:]字母与数字字符
    [:alpha:]字母字符(包括大小写字母)
    [:blank:]空格与制表符
    [:digit:]数字
    [:lower:]小写字母
    [:upper:]大写字母
    [:punct:]标点符号
    [:space:]包括换行符,回车等在内的所有空白

    四、正则练习

    创建文本文件demo.txt,输入一下内容

    Aieur45869Root0000
    9h847RkjfkIIIhello
    rootHllow88000dfjj
    8ikuioerhfhupliooking
    hello world
    192.168.0.254
    welcome to uplooking.
    abcderfkdjfkdtest
    rlllA899kdfkdfj
    iiiA848890ldkfjdkfj
    abc
    12345678908374
    123456@qq.com
    123456@163.com
    abcdefg@itcast.com23ed
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1、匹配ip地址
    # grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' demo.txt
    或者使用扩展正则
    # grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' demo.txt
    -P 可以让grep使用perl的正则表达式语法
    # grep -P '(\d{1,3}\.){3}[0-9]{1,3}' demo.txt
    
    2.、查找有数字的行(两种写法)
    # grep '[0-9]' demo.txt
    # grep -P '\d' demo.txt
    
    3.、查找一个数字和一个字母连起来的
    # grep -E  '([0-9][a-z])|([A-Z][0-9])|([a-z][0-9])|([0-9][A-Z])' demo.txt
    
    4、查找不以r开头的行
    # grep '^[^r]' demo.txt
    
    5、查找以数字开头的
    # grep '^[0-9]' demo.txt
    
    6、查找以大写字母开头的
    #  grep '^[A-Z]' demo.txt
    
    7、查找以小写字母开头的 
    # grep '^[a-z]' demo.txt
    
    8、查找以点结束的
    # grep '[.]$' demo.txt
    
    9、去掉空行
    # grep ^[^$] demo.txt
    
    10、查找完全匹配abc的行
    # grep '\' demo.txt
    
    11、查找A后有三个数字的行
    # grep -E 'A[0-9]{3}' demo.txt
    12、统计root在/etc/passwd里出现了几次
    [root@localhost ~]# grep -o 'root' /etc/passwd > tmp.txt
    [root@localhost ~]# wc -l tmp.txt
    4 tmp.txt
    
    13、用正则表达式找出自己的IP地址、广播地址、子网掩码
    # ifconfig | grep -P '(\d{1,3}\.){3}[0-9]{3}'
    
    
    
    
    • 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
  • 相关阅读:
    JavaWeb过滤器(Filter)详解,是时候该把过滤器彻底搞懂了(万字说明)
    web内容如何保护:如何有效地保护 HTML5 格式的视频内容?
    目标资产信息收集
    C++ QT QLocalSocket/QLocalServer基操
    Golang源码分析之golang/sync之singleflight
    10万字208道Java经典面试题总结(附答案)
    SkyWalking分布式系统应用程序性能监控工具-中
    如何在聊天记录中实时查找大量的微信群二维码
    Google Earth Engine(GEE)——用reducers来获取某一个区域得响应值并转化为列
    云原生大趋势下的容器化技术现状与发展
  • 原文地址:https://blog.csdn.net/weixin_53946852/article/details/126460900