grep

''  不識別特殊字符
""  識別特殊字符
``  非常識別

ifconfig ens33 |grep netmask
netstat -nt |grep tcp
last |grep root 
    grep
        grep -v  顯示不被模式匹配到的行
            grep -v ^$ file  非空行
            grep -v ^[[:space:]]$ file  去除空白行
            grep -v ^[[:space:]]*$ file
            grep -v "^$" /etc/profile |grep "^#"
            grep -v "/bin/bash$" /etc/psswd
        grep -n  顯示匹配的行號
            grep -n ^$ file  空行行號
        grep -c  統計匹配的行數
        grep -o  僅顯示匹配到的字符串
        grep -i  忽略字符大小寫
            grep -i ^s /proc/meminfo
        grep -q  靜默模式,不輸出任何信息  
            echo $?:0  找到  用在生產中只關心找到or找不到
            echo $?:1  找不到
        grep -A#  后幾行
        grep -B#  前幾行
        grep -C#  前后各幾行
        grep -e  多選項之間邏輯關系
        grep -w  匹配整個單詞
        grep -E = egrep  
        grep -F = fgrep 
            grep root /etc/passwd |grep bash  包含root和bash
            grep -e root -e wang /etc/passwd  或者包含root,或者包含wang
        字符匹配
            .               匹配任意單個字符
            []              匹配指定范圍內的任意單個字符
            [^]             匹配指定范圍外的任意單個字符
            [:alnum:]       字母和數字(a-zA-Z0-9)
            [:alpha:]       大寫、小寫
            [:lower:] [:upper:]     小寫 大寫
            [:bank:]        空白字符
            [:space:]       空白字符(范圍廣)
            [:cntrl:]       不可打印的控制字符(退格、刪除、警鈴)
            [:graph:]       可打印的非空白字符
            [:print:]       可打印字符
            [:punct:]       標點符號
            [:digit:]       10進制數字 
            [:xdigit:]      16進制數字
        匹配次數
            *                   匹配前面字符任意次(含0次),盡可能長的匹配
            .*                  任意長度的任意字符  目標的前、中、后
            \? or ?             0/1次
            \+ or +             至少1次
            \{n\} or {n}        n次
            \{m,n\} or {m,n}    m次到n次
            \{,n\}              ≦n次
            \{n,\}              ≧n次
            \                   轉義符
        位置錨定
            ^                   行首
            $                   行尾
            ^pattern$           匹配整行
              ^$                空行
              ^[[:space:]]$     空白行
            \< or \b            詞首
            \> or \b            詞尾
            \<pattern\>         整個單詞
        分組
            \(\)    將一個或者多個字符捆綁在一起,當做一個整體進行處理 \1 \2 \3
            \(string1\+\(string2\)*\)
                \1  string1\+\(string2\)*
                \2  string2
            后項引用    引用前面分組括號中的模式所匹配字符,而非模式本身
            a\|b or a|b     a或b
            \(c\|m\)at or (c|m)at       cat或mat
        練習
            grep "^rpc\>" /etc/passwd |cut -d: -f1,7
            grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
            grep "^[[:space:]]\+[^[:space:]]\{1,\}" /etc/grub2.cfg 
            netstat -tan |egrep -o "\<LISTEN>\[[:space:]]+$"
            grep "\<nologin\>" /etc/passwd |cut -d: -f1,3  不知道是否正確
                cut -d ":" -f1,3 /etc/passwd |egrep "\b[[:digit:]]{1,3}\b"
            echo bash testbash basher sh nologin |xargs -n1 useradd
                chsh -s /sbin/nologin nologin
            egrep "(^.*)\>.*/\1$" /etc/passwd
                cut -d: -f1,7 /etc/passwd |grep "^\([[:alpha:]]\+\>\).*/\1$"
            df |egrep -o "[0-9]{1,3}%" |sort -nr  不知道是否正確
                df|grep "^/dev/sd" |tr -s " " % |cut -d% -f5 |sort -nr 
                df|grep "^/dev/sd" |egrep -o "\<[[:digit:]]+%" |tr -d % |sort -nr 
            egrep "^(mage|root|wang)\>" /etc/passwd |cut -d: -f3,7
            egrep -o '^.*\>\(\)' /etc/rc.d/init.d/functions
            egrep -o '^[[:alnum:]_]+\(\)' /etc/rc.d/init.d/functions
            basename file  
            dirname file
                echo /etc/rc.d/init.d/functions |grep -Eo "[^/]+$"    or "[[:alnum:]]+$"
                echo /etc/rc.d/init.d/ |grep -Eo "[^/]+/?$"    or "[[:alnum:]]+/?$"
                echo /etc/rc.d/init.d/functions |egrep -o "([[:alnum:].]*/)+"
            last |tr -s " " |cut -d " " -f1,3 |egrep "root.*[[:digit:]]\.+" |sort |uniq -c 
            echo {0..1000} |egrep -o "\<[0-9]\>"
                echo {0..1000} |egrep -o "\<[1-9][0-9]\>"
                echo {0..1000} |egrep -o "\<1[1-9][0-9]\>"
                echo {0..1000} |egrep -o "\<2[0-4][0-9]\>"
                echo {0..1000} |egrep -o "\<25[0-5]\>"
            IP地址過濾(特殊IP也被過濾)
        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])\>
            ifconfig |grep "inet\" |tr -s " " |cut -d " " -f3 
            每個字母出現的次數和字母本身
                echo "welcome to lol linux" |grep -o '.' |sort |uniq -c |sort -nr       
    nmap
        沒有關鍵字的搜索
        黑客——開機機器、關機機器、機器服務——>IP、版本
        rpm -ivh /misc/cd/Packages/nmap-6...
        nmap -v -sP 172.17.252.0/24  掃描此網段開機的機器(up/down)
        提取開機的IP地址
            nmap -v -sP 172.17.252.0/24 |grep -B1 "Host is up" |grep for |cut -d " " -f5 > ip.log  
            wc -l ip.log
        掃描主機的服務
            nmap -v -A 172.17.0.1
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容