码迷,mamicode.com
首页 > 系统相关 > 详细

2018-03-12阿铭Linux学习

时间:2018-03-12 21:49:59      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:Linux学习

9.1 正则介绍 grep (上)

什么是正则

    正则就是一串有规律的字符串
    掌握好正则对于编写shell脚本有很大帮助
    各种编程语言中都有正则,原理是一样的

    本章主要学习 grep/egrep、sed、awk

掌握规律

grep 

    grep [-cinvABC] ‘work‘ filename
        -c  行数
        -i  不区分大小写
        -n  显示行号
        -v  取反
        -r  遍及所有子目录
        -A  后面跟数字,过滤出符合要求的行以及下面n行
        -B  后面跟数字,过滤出符合要求的行以及上面n行
        -C  后面跟数字,同时过滤出符合要求的行以及上下各n行

    [root@aming-01 grep]# grep -c ‘nologin‘ passwd
    16

    [root@aming-01 grep]# grep -n ‘nologin‘ passwd

    [root@aming-01 grep]# grep -i -c ‘nologin‘ passwd
    16
    [root@aming-01 grep]# grep -v -c ‘nologin‘ passwd
    4
    [root@aming-01 grep]# grep -vc ‘nologin‘ passwd
    4

    [root@aming-01 grep]# grep -v ‘nologin‘ passwd
    root:x:0:0:root:/root:/bin/bash
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt

    [root@aming-01 grep]# grep -A2 ‘root‘ passwd

9.2 grep (中)

grep/egrep 示例

    grep -n ‘root‘ /etc/passwd
    grep -nv ‘nologin‘ /etc/passwd
    grep ‘[0-9]‘ /etc/ininttab
    grep -v ‘[0-9]‘ /etc/inittab
    grep -v ‘^#‘ inittab
    grep -v ‘^#‘ inittab|grep -v ‘^$‘
    grep ‘^[^a-zA-Z]‘ test.txt
    grep ‘r.o‘ test.txt
    grep ‘oo‘ test.txt
    grep ‘.*‘ test.txt
    grep ‘o\{2\}‘ /etc/passwd
    egrep ‘o{2}‘ /etc/passwd
    egrep ‘o+‘ /etc/passwd
    egrep ‘oo?‘ /etc/passwd
    egrep ‘root|nologin‘ /etc/passwd
    egrep ‘(oo){2}‘ /etc/passwd

        [root@aming-01 grep]# grep -n ‘^#‘ inittab 
        1:# inittab is no longer used when using systemd.
        2:#
        3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
        4:#
        5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
        6:#
        7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
        8:#
        9:# multi-user.target: analogous to runlevel 3
        10:# graphical.target: analogous to runlevel 5
        11:#
        12:# To view current default target, run:
        16:# systemctl get-default
        17:#
        18:# To set a default target, run:
        19:# systemctl set-default TARGET.target
        20:#

        [root@aming-01 grep]# grep -nv ‘^#‘ inittab 
        13:adfafsafd
        14:ssss
        15:66666

        [root@aming-01 grep]# grep -n ‘[^0-9]‘ inittab 
        1:# inittab is no longer used when using systemd.
        2:#
        3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
        4:#
        5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
        6:#
        7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two maintargets:
        8:#
        9:# multi-user.target: analogous to runlevel 3
        10:# graphical.target: analogous to runlevel 5
        11:#
        12:# To view current default target, run:
        13:adfafsafd
        14:ssss
        16:# systemctl get-default
        17:#
        18:# To set a default target, run:
        19:# systemctl set-default TARGET.target
        20:#

        [root@aming-01 grep]# grep -n ‘^[^0-9]‘ inittab 
        1:# inittab is no longer used when using systemd.
        2:#
        3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
        4:#
        5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
        6:#
        7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
        8:#
        9:# multi-user.target: analogous to runlevel 3
        10:# graphical.target: analogous to runlevel 5
        11:#
        12:# To view current default target, run:
        13:adfafsafd
        14:ssss
        16:# systemctl get-default
        17:#
        18:# To set a default target, run:
        19:# systemctl set-default TARGET.target
        20:#

        [root@aming-01 grep]# grep -nv ‘^[^0-9]‘ inittab 
        15:66666

9.3 grep (下)

.  匹配.两边的字符中任意一个
*  重复*左边的字符0或无数个
+  重复+左边的字符1到无数个
? 重复?前面字符的0或1次
|  或者
()   找出群组字串
()+  多个重复群组字串的判别

    [root@aming-01 grep]# grep ‘r.o‘ passwd 
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

    [root@aming-01 grep]# grep ‘{2}‘ passwd 
    [root@aming-01 grep]# grep ‘o{2}‘ passwd 
    [root@aming-01 grep]# grep ‘o\{2\}‘ passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    bacula:x:133:133:Bacula Backup System:/var/spool/bacula:/sbin/nologin

    grep -E ‘o{2}‘ passwd  ===  egrep ‘o{2}‘ passwd 

    [root@aming-01 grep]# egrep ‘o+o‘ passwd

    [root@aming-01 grep]# grep -E ‘root|nologin‘ passwd

    [root@aming-01 data]# grep -r --include="*.php" ‘eval‘ /tmp/data
    /tmp/data/ext.php:eval      

2018-03-12阿铭Linux学习

标签:Linux学习

原文地址:http://blog.51cto.com/9298822/2085773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!