正则表达式

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

相关推荐

  • 8月3日作业

    课堂练习: 当用户xiaoming对/testdir 目录无执行权限时,意味着无法 做哪些操作? 不能进入目录,不能创建目录、文件 当用户xiaoqiang对/testdir 目录无读权限时,意味着无法做 哪些操作?  不能查看目录、文件 当用户wangcai 对/testdir 目录无写权限时,该目录下的只 读文件file1是否可修改和删除?&n…

    Linux干货 2016-08-07
  • linux下文件处理基础命令(自己预习瞎学的)

                      Linux下文件处理基础命令     1.创建文件   有时候我们会遇到需要创建一个空文件的情况。比如,应用程序可能希望在写…

    2017-07-15
  • find命令、归档压缩工具、xargs、exec、tar、xz、cpio

    find命令、归档压缩工具、xargs、exec、tar、xz、cpio find命令 Linux中的文件查找工具常见的有locate和find以及whereis.他们适用于不同的场合,如whereis只能用于搜索程序的二进制文件、源代码文件和man手册等相关文件,find和local能够查找磁盘上的所有文件(不包括proc和sys目录下的虚拟文件)。fin…

    Linux干货 2016-08-18
  • ansible实战示例

    要求:     使用ansible部署以下任务:     (1) 在VS部署主/备模型的keepalived + nginx的负载均衡;     (2) 在RS主机上部署httpd + php + php-mysql;     (3) 在第五台主机上部署mariadb-serve…

    Linux干货 2016-11-11
  • 文本处理学习小结

    抽取文本的工具 文件内容:less和cat 文件截取:head和tail 按列抽取:cut 按关键字抽取:grep 文件查看 复制标准输入到标准输出 文件查看命令:cat, tac,rev cat命令: cat [OPTION]… [FILE]… -E: 显示行结束符$ -n: 对显示出的每一行进行编号 -A:显示所有控制符 -b:非…

    Linux干货 2016-08-07
  • /etc/fstab及/boot分区文件恢复

    以centos6为例,/boot目录下有最为关键的开机启动所必须的内核文件、根文件系统驱动文件已经引导加载程序(bootloader)grub。当我们清空此文件夹之后关机,机器就不能正常启动了,这种情况下,可以借助光盘启动进入救援模式解决。具体步骤如下: 1. 开机进入救援模式 这里不像正常情况下,显示根文件系统挂载在/mnt/sysimage目录,而是提示…

    2017-07-09