正则表达式

正则表达式: 
            由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符
            字面意义,而表示控制或通配的功能
程序支持:  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

相关推荐

  • N21 第二周练习

    ####1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。复制:cp   移动:mv    删除:rm   cp:copy,复制文件或目录</br>  cp [OPTION]… SOURCE… DIRECTORY…

    Linux干货 2016-07-22
  • grep命令

    ]# chmod -R 700 /home/tuser1 ]# ll /home/tuser1/ total 0 drwx——. 2 root root 72 Nov 20 17:33 skel 新增用户与组: 显示/proc/meminfo: 显示shell 非 /sbin/nologin 的用户 找出/etc/passwd文件中一…

    Linux干货 2016-11-20
  • N25期–第六周作业

    请详细总结vim编辑器的使用并完成以下练习题 1、 复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; %s@^[[:space:]]\{1,\}.*@#&@ 2、 复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf…

    Linux干货 2017-01-17
  • 操作系统文件管理

      在现代计算机系统中,要用到大量的程序和数据,因内存容量有限,且不能长期保存,故而平时总是把它们以文件的形式存放在外存中,需要时再随时将它们调入内存。如果由用户直接管理外存上的文件,不仅要求用户熟悉外存特性,了解各种文件的属性,以及它们在外存上的位置,而且在多用户环境下,还必须能保持数据的安全性和一致性。显然,这是用户所不能胜任、也不愿…

    Linux干货 2015-04-13
  • 98-mysql数据备份

    一. 数据备份基础知识

    2016-11-20
  • grep基本正则表达式以及扩展正则表达式

    基本正则表达式: grep:Globel serach REgular expression and print out the line 作用:文本搜索工具,根据用户指定的“模式(过滤条件)”对目标文本逐行进行匹配检查,打印匹配到的行 模式:由正则表达式的元字符及文本字符所编写出的过滤条件 grep选项:       &nbs…

    Linux干货 2016-08-08