亚洲欧美精品沙发,日韩在线精品视频,亚洲Av每日更新在线观看,亚洲国产另类一区在线5

<pre id="hdphd"></pre>

  • <div id="hdphd"><small id="hdphd"></small></div>
      學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

      Linux grep命令用法

      時間: 志藝942 分享

        grep用于查找文件中符合字符串的那行。e.g. grep -nr "network_ssl" ./ [查找當(dāng)前文件夾下所有文件內(nèi)容,列出包含有 network_ssl該字串的行,并顯示行號],那么你知道Linux grep命令用法么?接下來是小編為大家收集的Linux grep命令用法,歡迎大家閱讀:

        Linux grep命令用法

        首先創(chuàng)建我們練習(xí)grep命令時需要用到的demo文件demo_file。

        $ cat demo_file

        THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

        this line is the 1st lower case line in this file.

        This Line Has All Its First Character Of The Word With Upper Case.

        www.2cto.com

        Two lines above this line is empty.

        And this is the last line.

        1.從單個文件中搜索指定的字串

        grep的基礎(chǔ)用法是如下例的從指定的文件中搜索特定的字串。

        語法:

        grep "literal_string" filename

        $ grep "this" demo_file

        this line is the 1st lower case line in this file.

        Two lines above this line is empty.

        And this is the last line.

        2. 在多個文件中檢索指定的字串

        語法:

        grep "string" FILE_PATTERN

        先拷貝demo_file為demo_file1。grep的結(jié)果在符合條件的行前將包括文件名。當(dāng)文件名包含元字符時,linux shell會將匹配的所有文件作為輸入到grep中去。

        $ cp demo_file demo_file1

        www.2cto.com

        $ grep "this" demo_*

        demo_file:this line is the 1st lower case line in this file.

        demo_file:Two lines above this line is empty.

        demo_file:And this is the last line.

        demo_file1:this line is the 1st lower case line in this file.

        demo_file1:Two lines above this line is empty.

        demo_file1:And this is the last line.

        3. 用 grep -i 進行大小寫無關(guān)的搜索

        語法:

        grep -i "string" FILE

        也是一個基本用法,對搜索的字串忽略大小寫,因此下例中匹配“the”, “THE” and “The”。

        $ grep -i "the" demo_file

        THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

        this line is the 1st lower case line in this file.

        This Line Has All Its First Character Of The Word With Upper Case.

        And this is the last line.

        4. 使用用正則表達(dá)式

        語法:

        grep "REGEX" filename

        如果你能有效地利用正則表達(dá)式,這是個很有用的特點。在下面的例子中,搜索全部以“lines”開始以“empty”結(jié)束的字串,如搜索“lines[之間任意字]empty” ,并且忽略大小寫。

        $ grep -i "lines.*empty" demo_file

        Two lines above this line is empty.

        正則表達(dá)式遵循的幾個重復(fù)的操作

        ? 最多匹配一次 www.2cto.com

        * 匹配零次或者任意多次

        + 匹配一次以上

        {n} 匹配n次

        {n,} 最少匹配n次

        {,m} 最多匹配m次

        {n,m} 匹配n到m次

        5. 用grep -w搜索整個詞,而不是詞中的部分字串

        使用-w選項搜索一個單詞,并且避免搜索到詞中的部分字串。

        下例搜索"is"。如果不加-w選項,將顯示“is”, “his”, “this” 等所有包含“is”的行。

        $ grep -i "is" demo_file

        THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

        this line is the 1st lower case line in this file.

        This Line Has All Its First Character Of The Word With Upper Case.

        Two lines above this line is empty.

        And this is the last line.

        下例使用了-w選項,請注意結(jié)果中不包含 “This Line Has All Its First Character Of The Word With Upper Case”, 雖然 “This”中包含“is”。

        $ grep -iw "is" demo_file www.2cto.com

        THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

        this line is the 1st lower case line in this file.

        Two lines above this line is empty.

        And this is the last line.

        6. 使用grep -A, -B and -C顯示之前、之后、前后的幾行

        當(dāng)使用grep搜索大文件時,顯示匹配行附近的多行數(shù)據(jù)是一個很有用的功能。

        創(chuàng)建如下文件

        $ cat demo_text

        4. Vim Word Navigation

        You may want to do several navigation in relation to the words, such as:

        * e - go to the end of the current word.

        * E - go to the end of the current WORD.

        * b - go to the previous (before) word.

        * B - go to the previous (before) WORD.

        * w - go to the next word.

        * W - go to the next WORD.

        WORD - WORD consists of a sequence of non-blank characters, separated with white space.

        word - word consists of a sequence of letters, digits and underscores.

        Example to show the difference between WORD and word

        * 192.168.1.1 - single WORD

        * 192.168.1.1 - seven words.

        6.1 顯示匹配行之后的N行

        -A

        語法:

        grep -A "string" FILENAME

        下例顯示匹配行和之后的3行數(shù)據(jù)

        $ grep -A 3 -i "example" demo_text

        Example to show the difference between WORD and word

        www.2cto.com

        * 192.168.1.1 - single WORD

        * 192.168.1.1 - seven words.

        6.2顯示匹配行之前的N行

        -B

        語法:

        grep -B "string" FILENAME

        下例顯示匹配行和之前的2行數(shù)據(jù)

        $ grep -B 2 "single WORD" demo_text

        Example to show the difference between WORD and word

        * 192.168.1.1 - single WORD

        6.3顯示匹配行前后的N行

        -C 顯示之前的n行,之后的n行數(shù)據(jù).

        $ grep -C 2 "Example" demo_text

        word - word consists of a sequence of letters, digits and underscores.

        Example to show the difference between WORD and word

        * 192.168.1.1 - single WORD

        7.通過GREP_OPTIONS高亮顯示搜索的字串

        如果你希望搜索的字串高亮顯示在結(jié)果中,可以試用以下的辦法。

        通過修改GREP_OPTIONS對搜索字串高亮顯示。

        $ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

        $ grep this demo_file

        this line is the 1st lower case line in this file.

        Two lines above this line is empty.

        And this is the last line.

        8. 用grep -r遞歸搜索全部的文件

        如果想查找當(dāng)前目前以及其子目錄的全部文件時,可以使用 -r 選項。如下例

        $ grep -r "ramesh" *

        9. 使用grep -v進行不匹配

        可以使用-v選項顯示不匹配搜索字串的行。下例顯示demo_text文件中不包含“go”的行

        $ grep -v "go" demo_text www.2cto.com

        4. Vim Word Navigation

        You may want to do several navigation in relation to the words, such as:

        WORD - WORD consists of a sequence of non-blank characters, separated with white space.

        word - word consists of a sequence of letters, digits and underscores.

        Example to show the difference between WORD and word

        * 192.168.1.1 - single WORD

        * 192.168.1.1 - seven words.

        10. 顯示不匹配全部模式的行

        語法:

        grep -v -e "pattern" -e "pattern"

        創(chuàng)建如下例子文件

        $ cat test-file.txt

        a

        b

        c

        d

        $ grep -v -e "a" -e "b" -e "c" test-file.txt

        d www.2cto.com

        11.用grep -c 統(tǒng)計匹配的行數(shù)

        語法:

        grep -c "pattern" filename

        $ grep -c "go" demo_text

        6

        統(tǒng)計不匹配的行數(shù)

        $ grep -v -c this demo_file

        4

        12. 用grep -l 只顯示文件名

        $ grep -l this demo_*

        demo_file

        demo_file1

        13. 只顯示匹配的字串

        缺省顯示匹配字串的所在行,可以使用-o選項只顯示匹配的字串。這項功能當(dāng)使用正則表達(dá)式時比較有用處。

        $ grep -o "is.*line" demo_file

        is line is the 1st lower case line

        is line

        is is the last line

        14. 顯示匹配的位置

        語法:

        grep -o -b "pattern" file

        $ cat temp-file.txt

        12345

        12345

        $ grep -o -b "3" temp-file.txt

        0:3

        6:3

        www.2cto.com

        注意: 以上輸出顯示的不是行內(nèi)的位置,而是整個文件中的字節(jié)byte位置

        15. 用 grep -n 在輸出時顯示行號

        行號從1開始

        $ grep -n "go" demo_text

        5: * e - go to the end of the current word.

        6: * E - go to the end of the current WORD.

        7: * b - go to the previous (before) word.

        8: * B - go to the previous (before) WORD.

        9: * w - go to the next word.

        10: * W - go to the next WORD.

        
      看了“Linux grep命令用法”還想看:

      1.Linux下如何使用grep命令搜索多個單詞

      2.linux grep搜索命令的使用方法

      3.Linux系統(tǒng)中怎么使用grep命令

      4.14個grep命令使用例子介紹

      Linux grep命令用法

      grep用于查找文件中符合字符串的那行。e.g. grep -nr network_ssl ./ [查找當(dāng)前文件夾下所有文件內(nèi)容,列出包含有 network_ssl該字串的行,并顯示行號],那么你知道Linux grep命令用法么?接下來是小編為大家收集的Linux grep命令用法,
      推薦度:
      點擊下載文檔文檔為doc格式

      精選文章

      • linux命令grep教程
        linux命令grep教程

        linux命令在系統(tǒng)中有兩種類型:內(nèi)置Shell命令和Linux命令。那么你知道linux命令grep教程么?接下來是小編為大家收集的linux命令grep教程,歡迎大家閱讀: linu

      • liunx grep命令常見用法
        liunx grep命令常見用法

        對于Linux系統(tǒng)來說,無論是中央處理器、內(nèi)存、磁盤驅(qū)動器、鍵盤、鼠標(biāo),還是用戶等都是文件,Linux系統(tǒng)管理的命令是它正常運行的核心,與之前的DOS命令

      • liunx強大的grep命令
        liunx強大的grep命令

        學(xué)習(xí)命令最好的方式就是模擬真實環(huán)境,寫出一些可能用到的命令組合,然后慢慢消化,起碼我就是這么過來的。一開始基本都是死記,然后再慢慢擴展視

      • linux文本內(nèi)容操作系列g(shù)rep命令詳解
        linux文本內(nèi)容操作系列g(shù)rep命令詳解

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

      2991740