04, grep and regular expressions

1, grep program

    Linux text processing Three Musketeers have under - grep sed awk

    grep: text line filtering tools

    sed: text line editor (stream editor)

    awk: report generator (output format of the present composition)

 

    grep

        It contains three commands: grep egrep fgrep, which are used for line mode (pattern) matching

         egrep = gerp -E // use extended regular expression matching

        the FAST = grep fgrep // use files matching the wildcard

        * Grep default text using regular expressions to match *

    grep usage:
        grep [the Option] ... the PATTERN [filename]

   

   grep's common option - the Option
            - E supports the use of extended regular expression (ERE) (regexp)
            -P use perl regular expression language search engine (every language of regular expression engines are not the same, even sed grep awk not the same engine used regexp)
            - I ignored size
            - V is selected from anti
            - O output only matching content (default output is matched to the line)
            --color = Auto syntax coloring
            - n-line number
            - W   match fixed word

    PATTERN-- regular expression
        functions: through some special characters to represent a class character, and then to the front of the command to execute; if you use special characters meaning in itself, it needs \ escape;

        Review: file wildcard (globbing)
            * [] [^]?

        1, character match
            .? Any one character
            [] any of the range of a character
            [^] outside the range of any character
            character class: [: digit:] [: alnum:] [: alpha:] [: lower:] [: Upper:] [: Space:] [: punct:]
        2, the number of match
            * matches the preceding character zero to many times
            \? Matches the preceding character zero to 1
            \ + Matches the preceding character 1 to n times
            \ {m \} arranged in front of the character m times
            A \ {. 7 \} AAAAAAA
            \ {m, n \} matches the preceding character m to the n-th
            \ {0, n \} matches the preceding character zero [0 to n times or not? (Not!)}
            \ {M, \} matches the preceding character at least m
        3, the position of the anchoring
            ^ anchors the first line
            $ anchor end of the line
            ^ [[: space:]] * $ blank line
            \ b anchor word The first and anchoring suffix
            \> anchor suffix
            \ <Anchoring the first word
            \ <root \> Rooter
        4, grouping
            abc * abcccc abc we want as a whole
            \ (\) Example: \ (abc \) * ABCABCABC abcccc
            ** grouping characteristics: By default, Linux system specifies the variable packet representation variables \ 1 \ 2 \ 3 ... (backward reference)

            \ (\ (Beginning of autumn \) passed \), \ (Hong Kong \) is still restless. Multi-zone in illegal gatherings, \ (violence \) demonstrators vandalism, blocked roads, or laser, bricks assaulting a police officer, throwing petrol bombs, and some even openly beaten mainland tourists and reporters. With the upgrade demonstrators, encouraged "anti-chaos in Hong Kong," the forces behind the scenes is slowly emerging.

            Example: \ (ab + \ (xy \) * \) where \ =. 1 + ab & \ (XY \) *, \ 2 = XY
                abbbbbxyxyxyabxy

        Add: extended regular expression
            [standard regular expression \ can be removed]
            groups: (after) the references \ 1 \ 2 \ 3 ...
            or: |
                Example: grep -E "(svm | vms ) "/ proc / cpuinfo // view the virtualization capabilities of the CPU

练习:
1、显示/proc/meminfo文件中大小s开头的行
    grep -i "^s" /proc/meminfo
2、显示/etc/passwd文件中不以/bin/bash结尾的行
    grep -v "/bin/bash$" /etc/passwd
3、显示/etc/passwd文件中UID号最大的用户的用户名
    sort -n -t: -k3 /etc/passwd | tail -1 | cut -d: -f1
4、如果用户root存在,显示其默认的shell程序
    grep "^root\>" /etc/passwd &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7 
    id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7 
5、找出/etc/passwd中的两位或三位数
    grep "[0-9]\{2,3\}"  /etc/passwd
        -w   //匹配固定单词 
        \<[[:digit:]]\{2,3\}\>
        \b[0-9]\{2,3\}\b
6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面为非空白字符的行
    grep "^[[:space:]]\+.*[^[:space:]]$" /etc/rc.d/rc.sysinit
7、找出”netstat -tan”命令的结果中,以“LISTEN”后跟0、1或多个空白字符结尾的行
    netstat -tan | grep "LISTEN[[:space:]]*$"
8、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin):而后找出/etc/passwd文件中用户名同shell名的行
    useradd bash           /bin/bash
    useradd testbash
    useradd basher
    useradd -s /sbin/nologin nologin
    grep "\(^[[:alnum:]]\+\>\).*\1$" /etc/passwd
9、IP地址如何匹配?
    ip地址:
        0-255.0-255.0-255.0-255
        0-255
        2 0-4 0-9  2[0-4][0-9]
        2 5 0-5    25[0-5]
        1 0-9 0-9  1[0-9][0-9]
        0 0-9 0-9  [0-9][0-9]
        0 0    0-9  [0-9]
    
grep -owE "((([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])){1}[[:space:]]"

 

 

 

 

Guess you like

Origin www.cnblogs.com/cnxy168/p/11409019.html