系统符号

2019-07-22  本文已影响0人  张新雨有点胖噢

系统基础正则

正则符号主要是为三剑客grep sed awk命令做辅助作用
基础正则 basic regular expression 简称 BRE

  1. ^符号 代表以什么开头2.
[root@zin ~]# cat oldboy.txt 
 am oldboy teacher!
        I teach linux.
        
        I like badminton ball ,billiard ball and chinese chess!
        my blog is http://oldboy.blog.51cto.com
        our site is http://www.etiantian.org
        my qq num is 49000448.
        
        not 4900000448.
        my god ,i am not oldbey,but OLDBOY!
[root@zin ~]# grep '^ a' oldboy.txt 
 am oldboy teacher!
  1. $符号代表以什么结尾
[root@zin ~]# grep 'm$' oldboy.txt 
my blog is http://oldboy.blog.51cto.com
  1. ^$ 代表空行
  2. . 匹配一个且只有一个字符 和通配*号差不太多
    . 符号拥有三个特特性
    1.会按照行进行匹配
    2.会匹配信息的贪婪特性(默认不加匹配项会一个一个的匹配文件所有内容)
    3.利用grep -o可以查看匹配信息过程
[root@zin ~]# grep '.d' oldboy.txt 
 am oldboy teacher!
        I like badminton ball ,billiard ball and chinese chess!
        my blog is http://oldboy.blog.51cto.com
        my god ,i am not oldbey,but OLDBOY!
  1. *符号 匹配前面一个字符连续出现 0次或者多次
    因为*号是匹配0或者多次 所以会将所有内容显示出来 因为没有的会默认出现0次
[root@zin ~]# grep "d*" oldboy.txt 
 am oldboy teacher!
        I teach linux.
        
        I like badminton ball ,billiard ball and chinese chess!
        my blog is http://oldboy.blog.51cto.com
        our site is http://www.etiantian.org
        my qq num is 49000448.
        
        not 4900000448.
        my god ,i am not oldbey,but OLDBOY!
ZZ
  1. .*符号 匹配所有内容
[root@zin ~]# grep ".*" oldboy.txt 
 am oldboy teacher!
        I teach linux.
        
        I like badminton ball ,billiard ball and chinese chess!
        my blog is http://oldboy.blog.51cto.com
        our site is http://www.etiantian.org
        my qq num is 49000448.
        
        not 4900000448.
        my god ,i am not oldbey,but OLDBOY!
ZZ
  1. []方括号符号 符号里的内容会分别过滤
[root@zin ~]# grep '[as]' oldboy.txt 
 am oldboy teacher!
        I teach linux.
        I like badminton ball ,billiard ball and chinese chess!
        my blog is http://oldboy.blog.51cto.com
        our site is http://www.etiantian.org
        my qq num is 49000448.
        my god ,i am not oldbey,but OLDBOY!
  1. [^] 匹配取反
[root@zin ~]# grep '[^0-9]' oldboy.txt 
 am oldboy teacher!
        I teach linux.
        I like badminton ball ,billiard ball and chinese chess!
        our site is http://www.etiantian.org
        my god ,i am not oldbey,but OLDBOY!
匹配的是数值0-9 所有把所有有数字
所有所有有数值的都没显示 

扩展正则

扩展正则: extended regular expression 缩写 ERE
扩展正则三剑客中awk可以无条件使用
其中grep需要变形成egrep或者加上参数 grep -E才可使用
sed需要加参数-r才可以使用 sed -r

  1. +符号 匹配前一个字符出现1次或者多次
    因为是1~多次 所有不会像*号一样出现显示没有匹配项也出现的问题
[root@zin ~]# egrep "0+" oldboy.txt 
  our site is 0 http://www.etiantian.org
  my qq num is 49000448.
  1. | 匹配多个不同的字符串信息
[root@zin ~]# grep -E 'not|and' oldboy.txt 
      I like badminton ball ,billiard ball and chinese chess!
      not 4900000448.
      my god ,i am not oldbey,but OLDBOY!
  1. ()小括号符号 两个作用
    1.将多个信息进行整合过滤
    2.实现后项引用前向进行替换
例
[root@zin ~]# echo "123456"|sed -r 's#([0-9]+)#<\1>#g'
将前向() 利用\1 引用过来赋予新的值或任务
<123456>
  1. {} 匹配括号前一个字符出现的次数
    1.xxx{n,m} 匹配xxx出现n次到m次
    2.xxx{n}匹配xxx出现n次
    3.xxx{n,}匹配xxx最少n次 上不限次数
    4.xxx{,m}匹配xxx最少0次 上限m次
  2. ? 前一个字符连续出现0次或一次
[root@zin ~]# grep -E '?u' oldboy.txt 
        I teach linux.
        our site is http://www.etiantian.org
        my qq num is 49000448.
        my god ,i am not oldbey,but OLDBOY!
上一篇 下一篇

猜你喜欢

热点阅读