<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • linuxgrep搜索命令的用法

            這篇文章主要介紹“l(fā)inux grep搜索命令的用法”,在日常操作中,相信很多人在linux grep搜索命令的用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”linux grep搜索命令的用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

            這篇文章主要介紹“l(fā)inux grep搜索命令的用法”,在日常操作中,相信很多人在linux grep搜索命令的用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”linux grep搜索命令的用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

            創(chuàng)新互聯(lián)是一家專業(yè)提供濟水街道企業(yè)網(wǎng)站建設,專注與網(wǎng)站設計制作、成都做網(wǎng)站、H5開發(fā)、小程序制作等業(yè)務。10年已為濟水街道眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設公司優(yōu)惠進行中。

            用&lsquo;grep&rsquo;搜索文本文件
            如果您要在幾個文本文件中查找一字符串,可以使用&lsquo;grep&rsquo;命令。&lsquo;grep&rsquo;在文本中搜索指定的字符串。

            假設您正在&lsquo;/usr/src/linux/Documentation&rsquo;目錄下搜索帶字符串&lsquo;magic&rsquo;的文件:
            $ grep magic /usr/src/linux/Documentation/*
            sysrq.txt:* How do I enable the magic SysRQ key?

            sysrq.txt:* How do I use the magic SysRQ key?

            其中文件&lsquo;sysrp.txt&rsquo;包含該字符串,討論的是 SysRQ 的功能。

            默認情況下,&lsquo;grep&rsquo;只搜索當前目錄。如果此目錄下有許多子目錄,&lsquo;grep&rsquo;會以如下形式列出:

            grep: sound: Is a directory

            這可能會使&lsquo;grep&rsquo;的輸出難于閱讀。這里有兩種解決的辦法:

            明確要求搜索子目錄:grep -r

            或忽略子目錄:grep -d skip

            當然,如果預料到有許多輸出,您可以通過 管道 將其轉(zhuǎn)到&lsquo;less&rsquo;上閱讀:

            $ grep magic /usr/src/linux/Documentation/* | less

            這樣,您就可以更方便地閱讀。

            有一點要注意,您必需提供一個文件過濾方式(搜索全部文件的話用 *)。如果您忘了,&lsquo;grep&rsquo;會一直等著,直到該程序被中斷。如果您遇到了這樣的情況,按 <CTRL c> ,然后再試。

            下面是一些有意思的命令行參數(shù):

            grep -i pattern files :不區(qū)分大小寫地搜索。默認情況區(qū)分大小寫,

            grep -l pattern files :只列出匹配的文件名,

            grep -L pattern files :列出不匹配的文件名,

            grep -w pattern files :只匹配整個單詞,而不是字符串的一部分(如匹配&lsquo;magic&rsquo;,而不是&lsquo;magical&rsquo;),

            grep -C number pattern files :匹配的上下文分別顯示[number]行,

            grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,

            grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。

            這里還有些用于搜索的特殊符號:
            \< 和 \> 分別標注單詞的開始與結(jié)尾。

            例如:

            grep man * 會匹配 &lsquo;Batman&rsquo;、&lsquo;manic&rsquo;、&lsquo;man&rsquo;等,

            grep '\<man' * 匹配&lsquo;manic&rsquo;和&lsquo;man&rsquo;,但不是&lsquo;Batman&rsquo;,

            grep '\<man\>' 只匹配&lsquo;man&rsquo;,而不是&lsquo;Batman&rsquo;或&lsquo;manic&rsquo;等其他的字符串。

            '^':指匹配的字符串在行首,

            '$':指匹配的字符串在行尾,

            如果您不習慣命令行參數(shù),可以試試圖形界面的&lsquo;grep&rsquo;,如 reXgrep 。這個

            軟件

            提供 AND、OR、NOT 等語法,還有漂亮的按鈕 :-) 。如果您只是需要更清楚的輸出,不妨試試 fungrep 。
            1.作用

            Linux系統(tǒng)中g(shù)rep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權(quán)限是所有用戶。
            2.格式

            grep [options]
            3.主要參數(shù)
            [options]主要參數(shù):
            -c:只輸出匹配行的計數(shù)。
            -I:不區(qū)分大 小寫(只適用于單字符)。
            -h(huán):查詢多文件時不顯示文件名。
            -l:查詢多文件時只輸出包含匹配字符的文件名。
            -n:顯示匹配行及 行號。
            -s:不顯示不存在或無匹配文本的錯誤信息。
            -v:顯示不包含匹配文本的所有行。
            pattern正則表達式主要參數(shù):
            \: 忽略正則表達式中特殊字符的原有含義。
            ^:匹配正則表達式的開始行。
            $: 匹配正則表達式的結(jié)束行。
            \<:從匹配正則表達 式的行開始。
            \>:到匹配正則表達式的行結(jié)束。
            [ ]:單個字符,如[A]即A符合要求 。
            [ - ]:范圍,如[A-Z],即A、B、C一直到Z都符合要求 。
            。:所有的單個字符。

            * :有字符,長度可以為0。
            4.grep命令使用簡單實例
            $ grep &lsquo;test&rsquo; d*
            顯示所有以d開頭的文件中包含 test的行。
            $ grep &lsquo;test&rsquo; aa bb cc
            顯示在aa,bb,cc文件中匹配test的行。
            $ grep &lsquo;[a-z]\{5\}&rsquo; aa
            顯示所有包含每個字符串至少有5個連續(xù)小寫字符的字符串的行。
            $ grep &lsquo;w\(es\)t.*\1&prime; aa

            如果west被匹配,則es就被存儲到內(nèi)存中,并標記為1,然后搜索任意個字符(.*),這些字符后面緊跟著 另外一個es(\1),找到就顯示該行。如果用egrep或grep -E,就不用”\”號進行轉(zhuǎn)義,直接寫成&rsquo;w(es)t.*\1&prime;就可以了。
            5.grep命令使用復雜實例
            假設您正在&rsquo;/usr/src/Linux/Doc&rsquo;目錄下搜索帶字符 串&rsquo;magic&rsquo;的文件:
            $ grep magic /usr/src/Linux/Doc/*
            sysrq.txt:* How do I enable the magic SysRQ key?
            sysrq.txt:* How do I use the magic SysRQ key?
            其中文件&rsquo;sysrp.txt&rsquo;包含該字符串,討論的是 SysRQ 的功能。
            默認情況下,&rsquo;grep&rsquo;只搜索當前目錄。如果 此目錄下有許多子目錄,&rsquo;grep&rsquo;會以如下形式列出:
            grep: sound: Is a directory
            這可能會使&rsquo;grep&rsquo; 的輸出難于閱讀。這里有兩種解決的辦法:
            明確要求搜索子目錄:grep -r
            或忽略子目錄:grep -d skip
            如果有很多 輸出時,您可以通過管道將其轉(zhuǎn)到&rsquo;less&rsquo;上閱讀:
            $ grep magic /usr/src/Linux/Documentation/* | less

            這樣,您就可以更方便地閱讀。

            有一點要注意,您必需提供一個文件過濾方式(搜索全部文件的話用 *)。如果您忘了,&rsquo;grep&rsquo;會一直等著,直到該程序被中斷。如果您遇到了這樣的情況,按 <CTRL c> ,然后再試。
            下面還有一些有意思的命令行參數(shù):
            grep -i pattern files :不區(qū)分大小寫地搜索。默認情況區(qū)分大小寫,
            grep -l pattern files :只列出匹配的文件名,
            grep -L pattern files :列出不匹配的文件名,
            grep -w pattern files :只匹配整個單詞,而不是字符串的一部分(如匹配&rsquo;magic&rsquo;,而不是&rsquo;magical&rsquo;),
            grep -C number pattern files :匹配的上下文分別顯示[number]行,
            grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,

            grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。

            grep -n pattern files  即可顯示行號信息

            grep -c pattern files  即可查找總行數(shù)
            這里還有些用于搜索的特殊符號:
            \< 和 \> 分別標注單詞的開始與結(jié)尾。
            例如:
            grep man * 會匹配 &lsquo;Batman&rsquo;、&rsquo;manic&rsquo;、&rsquo;man&rsquo;等,
            grep &lsquo;\<man&rsquo; * 匹配&rsquo;manic&rsquo;和&rsquo;man&rsquo;,但不是&rsquo;Batman&rsquo;,
            grep &lsquo;\<man\>&rsquo; 只匹配&rsquo;man&rsquo;,而不是&rsquo;Batman&rsquo;或&rsquo;manic&rsquo;等其他的字符串。
            &lsquo;^&rsquo;:指匹配的字符串在行首,

            &lsquo;$&rsquo;:指匹配的字符串在行 尾,

            Grep 命令 用法大全
            1、 參數(shù):
            -I :忽略大小寫
            -c :打印匹配的行數(shù)
            -l :從多個文件中查找包含匹配項
            -v :查找不包含匹配項的行

            -n:打印包含匹配項的行和行標
            2、RE(正則表達式)
            \ 忽略正則表達式中特殊字符的原有含義
            ^ 匹配正則表達式的開始行
            $ 匹配正則表達式的結(jié)束行
            \< 從匹配正則表達式的行開始
            \> 到匹配正則表達式的行結(jié)束
            [ ] 單個字符;如[A] 即A符合要求
            [ - ] 范圍 ;如[A-Z]即A,B,C一直到Z都符合要求
            . 所有的單個字符

            * 所有字符,長度可以為0
             3、舉例
            # ps -ef | grep in.telnetd

             root 19955 181 0 13:43:53 ? 0:00 in.telnetd
             # more size.txt size文件的內(nèi)容
            b124230
             b034325
             a081016
             m7187998
             m7282064
             a022021
             a061048
             m9324822
             b103303
             a013386
             b044525
             m8987131
             B081016
             M45678
             B103303

             BADc2345
             # more size.txt | grep '[a-b]' 范圍 ;如[A-Z]即A,B,C一直到Z都符合要求
            b124230
             b034325
             a081016
             a022021
             a061048
             b103303
             a013386
             b044525
             # more size.txt | grep '[a-b]'*
             b124230
             b034325
             a081016
             m7187998
             m7282064
             a022021
             a061048
             m9324822
             b103303
             a013386
             b044525
             m8987131
             B081016
             M45678
             B103303

             BADc2345
             # more size.txt | grep 'b' 單個字符;如[A] 即A符合要求
            b124230
             b034325
             b103303
             b044525
             # more size.txt | grep '[bB]'
             b124230
             b034325
             b103303
             b044525
             B081016
             B103303

             BADc2345
             # grep 'root' /etc/group
             root::0:root
             bin::2:root,bin,daemon
             sys::3:root,bin,sys,adm
             adm::4:root,adm,daemon
             uucp::5:root,uucp
             mail::6:root
             tty::7:root,tty,adm
             lp::8:root,lp,adm
             nuucp::9:root,nuucp

             daemon::12:root,daemon
             # grep '^root' /etc/group 匹配正則表達式的開始行

            root::0:root
             # grep 'uucp' /etc/group
             uucp::5:root,uucp

             nuucp::9:root,nuucp
             # grep '\<uucp' /etc/group

             uucp::5:root,uucp
             # grep 'root$' /etc/group 匹配正則表達式的結(jié)束行
            root::0:root

             mail::6:root # more size.txt | grep -i 'b1..*3' -i :忽略大小寫


            b124230
             b103303

             B103303 # more size.txt | grep -iv 'b1..*3' -v :查找不包含匹配項的行


            b034325
             a081016
             m7187998
             m7282064
             a022021
             a061048
             m9324822
             a013386
             b044525
             m8987131
             B081016
             M45678

             BADc2345 # more size.txt | grep -in 'b1..*3'

             1:b124230
             9:b103303

             15:B103303 # grep '$' /etc/init.d/nfs.server | wc -l

             128

             # grep '\$' /etc/init.d/nfs.server | wc &ndash;l 忽略正則表達式中特殊字符的原有含義
            15
             # grep '\$' /etc/init.d/nfs.server
             case "$1" in
             >/tmp/sharetab.$$
             [ "x$fstype" != xnfs ] &&
             echo "$path\t$res\t$fstype\t$opts\t$desc"
             >>/tmp/sharetab.$$
             /usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$
             /usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab
             if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)'
             if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] &&
             if [ $startnfsd -ne 0 ]; then
             elif [ ! -n "$_INIT_RUN_LEVEL" ]; then
             while [ $wtime -gt 0 ]; do
             wtime=`expr $wtime - 1`
             if [ $wtime -eq 0 ]; then

             echo "Usage: $0 { start | stop }"

             # more size.txt
             the test file
             their are files

             The end
             # grep 'the' size.txt
             the test file

             their are files
             # grep '\<the' size.txt
             the test file

             their are files
             # grep 'the\>' size.txt

             the test file
             # grep '\<the\>' size.txt

             the test file
             # grep '\<[Tt]he\>' size.txt

             the test file
             1,簡介
             使用正則表達式的一個多用途文本搜索工具.這個php?name=%C3%FC%C1%EE" onclick="tagshow(event)" class="t_tag">命令本來是ed行編輯器中的一個php?name=%C3%FC%C1%EE" onclick="tagshow(event)" class="t_tag">命令/過濾器:
                     g/re/p -- global - regular expression - print.
            基本格式
            grep pattern [file...]
             (1)grep 搜索字符串 [filename]
             (2)grep 正則表達式 [filename]
            在文件中搜索所有 pattern 出現(xiàn)的位置, pattern 既可以是要搜索的字符串,也可以是一個正則表達式.

            注意:在輸入要搜索的字符串時最好使用雙引號/而在模式匹配使用正則表達式時,注意使用單引號
            2,grep的選項
                -c 只輸出匹配行的計數(shù)
                -i 不區(qū)分大小寫(用于單字符)
                -n 顯示匹配的行號
                -v 不顯示不包含匹配文本的所以有行
                -s 不顯示錯誤信息
                -E 使用擴展正則表達式

                更多的選項請查看:man grep

             3,常用grep實例
            (1)多個文件查詢

                grep "sort" *.doc       #見文件名的匹配
            (2)行匹配:輸出匹配行的計數(shù)

                grep -c "48" data.doc   #輸出文檔中含有48字符的行數(shù)
            (3)顯示匹配行和行數(shù)

                grep -n "48" data.doc       #顯示所有匹配48的行和行號
            (4)顯示非匹配的行

                grep -vn "48" data.doc      #輸出所有不包含48的行
            (4)顯示非匹配的行

                grep -vn "48" data.doc      #輸出所有不包含48的行
            (5)大小寫敏感

                grep -i "ab" data.doc       #輸出所有含有ab或Ab的字符串的行

            4, 正則表達式的應用
            (1)正則表達式的應用 (注意:最好把正則表達式用單引號括起來)

                 grep '[239].' data.doc      #輸出所有含有以2,3或9開頭的,并且是兩個數(shù)字的行
            (2)不匹配測試

                grep '^[^48]' data.doc      #不匹配行首是48的行
            (3)使用擴展模式匹配

                grep -E '219|216' data.doc
             (4) ...

                 這需要在實踐中不斷應用和總結(jié),熟練掌握正則表達式。
            5, 使用類名
             可以使用國際模式匹配的類名:
            [[:upper:]]   [A-Z]
             [[:lower:]]   [a-z]
             [[:digit:]]   [0-9]
             [[:alnum:]]   [0-9a-zA-Z]
             [[:space:]]   空格或tab

             [[:alpha:]]   [a-zA-Z]
             (1)使用

                grep '5[[:upper:]][[:upper:]]' data.doc     #查詢以5開頭以兩個大寫字母結(jié)尾的行

            當前題目:linuxgrep搜索命令的用法
            本文鏈接:http://www.jbt999.com/article14/ssdge.html

            成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、微信小程序、Google、搜索引擎優(yōu)化自適應網(wǎng)站、做網(wǎng)站

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

            手機網(wǎng)站建設

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 欧美另类| 小黄片网站 | 91九色TS另类国产人妖 | 操逼大全| 日本艹逼|