正则表达式

正则表达式: 
            由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符
            字面意义,而表示控制或通配的功能
程序支持:  grep,sed,awk,vim,less,nginx,varnish 等

元字符分类:字符匹配,匹配次数,位置锚定,分组
man 7 regex 获取帮助

. 匹配任意单个字符 (也可以ls a?   ls a?? ls *匹配任意一个单一字符)# echo adc |grep .

                                             # grep roo. /etc/passwd

正则表达式

[] 匹配指定范围内的任意单个字符      # echo ac bc fc |grep [ab]c

                                                   # echo 1 2 3 4 5 6 3 7 fc |grep [1-5]

                                                   # echo 1 4 5 6 7 v 1l a b n |grep [1-6][a-l]
                                                  正则表达式

[^] 匹配指定范围外的任意单个字符
[:alnum:] 或 [0-9a-zA-Z] # echo a d c b 1 2 3 | grep “[[:alnum:]]” # echo a d c b 1 2 3 ? | grep  [^[:alnum:]]
[:alpha:] 或 [a-zA-Z]
[:upper:] 或 [A-Z] 大写字母 # echo a d c b 1 2 3 E ? | grep  “[[:digit:][:upper:]]”
[:lower:] 或 [a-z]
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)
[:cntrl:] 不可打印的控制字符(退格、删除、警铃…)
[:digit:] 十进制数字 或[0-9] # echo a d c b 1 2 3 | grep [[:digit:]]
[:xdigit:]十六进制数字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 标点符号

匹配次数: 用在要指定次数的字符的字符后面,用于指定前面的字符要出现的次数
            先编写一个文件 cat > regex.txt 编写内容
            匹配前面的字符任意任意次,包含0次 # cat regex.txt|grep g[o]le
               贪婪模式:尽可能长的匹配
            .任意长度的任意字符               # cat regex.txt|grep g.le
            \?匹配其前面的字符0或1次           # cat regex.txt|grep “g[o]\?gle”
            +匹配其前面的字符至少1次          # cat regex.txt|grep “g[o]+gle”
            {n}匹配其前面的字符n次           # cat regex.txt|grep “g[o]{4}gle”
            {m,n}匹配其前面的字符至少m次 至多n次 # cat regex.txt|grep “g[o]{1,10}gle”
            {,n}匹配其前面的字符 至多n次     # cat regex.txt|grep “g[o]{,10}gle”
            {n,}匹配其前面的字符 至少n次    # cat regex.txt|grep “g[o]{1,}gle”
        
             # cat regex.txt|grep g[o]le     gole
             # cat regex.txt|grep g[o][o]le  goole

            ^行首锚定,用于模式的最左侧 # grep ^root /etc/passwd
            $行尾锚定,用于模式的最右侧 # grep /bin/bash$ /etc/passwd
            ^PATTERN$用于模式匹配整行
                ^$空行 #cat /etc/httpd/conf/httpd.conf |grep -ve “^$” -e “^#”
                       #cat /etc/httpd/conf/httpd.conf |grep -v “^$|^#”
                ^[[:space:]]*$空白行
            \<或\b 词首锚定,用于单词模式的左侧
            \>或\b 词尾锚定,用于单词模式的右侧
            \<PATTERN\>匹配整个单词
            # grep  “\<root\>” /etc/passwd
            # grep  “^root\>” /etc/passwd
            # grep  “^root\b” /etc/passwd

            # grep  “^root:\b” /etc/passwd

正则表达式

分组 :()将一个或多个字符捆绑在一起,当做一个整体进行处理 如: “(root)+”

           # grep  “(\root)” /etc/passwd
            
       \1表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符
         如: (string1+string2)
)
              \1:string1+(string2)
              \2 :string2
           写一个脚本 test.txt
            #cat test.txt |grep “(r..t)”
            #cat test.txt |grep “(r..t).
\1″ 必须以root开头并以root结尾 
             root,dig,raat,root,rooo 前四个显示 必须以root开头并以root结尾 
             root,dig,raat,rooo,root全部显示  必须以root开头并以root结尾 

       或者| a|: a或b C|cat: C或cat

练习:
1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
        #cat /proc/meminfo|grep “^[Ss]”
        #cat /proc/meminfo|grep -i “^s”
        #cat /proc/meminfo|grep -e ^s -e ^S
        #cat /proc/meminfo|grep “^s|^S”
        #cat /proc/meminfo|grep “^[s|S]”
正则表达式
2、显示/etc/passwd文件中不以/bin/bash结尾的行
        #grep -v “/bin/bash$” /etc/passwd

3、显示用户rpc默认的shell程序
        #grep “^rpc\>”   /etc/passwd  | cut -d : -f7 
        #grep -w “^rpc”   /etc/passwd  | cut -d : -f7 
正则表达式
4、找出/etc/passwd中的两位或三位数
        #cat /etc/passwd |grep -o “\<[0-9]{2,3}\>”

正则表达式

5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白
字符开头的且后面存非空白字符的行

        #cat /etc/grub2.cfg  |grep “^[[:space:]]\+[^[:space:]]”

正则表达式

6、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多
个空白字符结尾的行

        #netstat -tan|grep “\<LISTEN\>[[:space:]]*$” 

正则表达式

7、显示CentOS7上所有系统用户的用户名和UID
        #cat /etc/passwd |cut -d: -f1,3 |grep “\<[[:digit:]]{1,3}\>”$
正则表达式
8、添加用户bash、testbash、basher、sh、nologin(其shell
为/sbin/nologin),找出/etc/passwd用户名同shell名的行
        #cat /etc/passwd | grep “(^.)\>.\/\1$”
正则表达式
9、仅利用df和grep和sort,取出磁盘各分区利用率,并从大到小排序
        #df |grep ^/dev/sd |grep -o “\b[[:digit:]]{1,3}\b%”|sort -rn

正则表达式
扩展的正则表达式
egrep 或grep -E 可以省略\ 
  除了\<    \> 
       \b    \b
  不可以省略

作业:
1、显示三个用户root、mage、wang的UID和默认shell
       #cat /etc/passwd|grep -E “^(root|wang|mage)\>”|cut -d : -f3,7
       #cat /etc/passwd|grep -E -w “^(root|wang|mage)”|cut -d : -f3,7
正则表达式
2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
      # cat /etc/rc.d/init.d/functions | egrep “^[[:alpha:]_]+\>()” 
      # cat /etc/rc.d/init.d/functions | grep  -o “^.*[:graph:]

      # cat /etc/rc.d/init.d/functions | grep  -o “^.*\>\(\)”

正则表达式

3、使用egrep取出/etc/rc.d/init.d/functions中其基名

   # echo /etc/rc.d/init.d/functions |egrep “[^/]+/?$”

正则表达式

4、使用egrep取出上面路径的目录名
 # echo “/etc/rc.d/init.d/functions” |egrep -o ‘.*/\b’

 # echo “/etc/rc.d/init.d/functions” |egrep -o ‘.*/\<‘

正则表达式

5、统计last命令中以root登录的每个主机IP地址登录次数

# last |grep “^root\>”|egrep -o “([0-9]{1,3}\.){3}[0-9]{1,3}” |sort|uniq -c

正则表达式

6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
  #echo {0..300}|egrep -o “\<25[0-5]\>”
      [0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]
7、显示ifconfig命令结果中所有IPv4地址

ifconfig | egrep -o “\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>”

正则表达式
8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
      # echo “welcome to magedu linux”|grep -o . |sort|uniq -c |sort -nr
正则表达式

原创文章,作者:MOMO,如若转载,请注明出处:http://www.178linux.com/83520

(0)
MOMOMOMO
上一篇 2017-08-05 19:45
下一篇 2017-08-05 20:34

相关推荐

  • ☞磁盘管理{分区表备份与恢复;修复fstab记录错误;制作swap分区;磁盘配额演示;}

    磁盘管理{ 分区表备份与恢复;错误fstab修复;swap分区;磁盘配额;}

    Linux干货 2016-09-04
  • 20160804正则表达式作业

    正则表达式表示18位身份证号 正则表达式表示手机号 正则表达式表示邮箱 Ø  截取出ifconfig命令结果中所有的本机的ipv4地址   Ø  查看分区中使用率最大的百分比 Ø  查看UID最大的用户名、UID以及shell类型 Ø  查看/tmp权限,并且以数字方式显示 思路:先找到如何才能查看到/tmp…

    Linux干货 2016-08-08
  • PHP数组实际占用内存大小的分析

    我们在前面的php高效写法提到,尽量不要复制变量,特别是数组。一般来说,PHP数组的内存利用率只有 1/10, 也就是说,一个在C语言里面100M 内存的数组,在PHP里面就要1G。下面我们可以粗略的估算PHP数组占用内存的大小,首先我们测试1000个元素的整数占用的内存: <?php      &nb…

    系统运维 2015-05-28
  • 分享 (History,Ls,感悟 )

    1.History 选项   -c: 清空命令历史   -d offset: 删除历史中指定的第offset个命令    n: 显示最近的n条历史   -a: 追加本次会话新执行的命令历史列表至历史文件   -n: 读历史文件中未读过的行到历史列表   -r: 读历史文件附加到历史列表 &…

    2017-07-15
  • grep、find练习

    1.显示当前系统上root,fedora或user1用户的默认shell [root@study ~]# cat /etc/passwd|grep -E "^(root|fedora|user1)"|cut -d: -f7 /bin/bash /bin/bash /bi…

    Linux干货 2016-11-27