精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

      linux中的grep命令的用法詳解

      時(shí)間: 佳洲1085 分享

        linxu下的grep命令是經(jīng)常使用到的命令之一。下面由學(xué)習(xí)啦小編為大家整理了linux的grep命令的用法詳解的相關(guān)知識(shí),希望對(duì)大家有幫助!

        一、linux中的grep命令的用法詳解

        作為linux中最為常用的三大文本(awk,sed,grep)處理工具之一,掌握好其用法是很有必要的。

        首先談一下grep命令的常用格式為:【grep [選項(xiàng)] ”模式“ [文件]】

        常用選項(xiàng):

        -E :開(kāi)啟擴(kuò)展(Extend)的正則表達(dá)式。

        -i :忽略大小寫(xiě)(ignore case)。

        -v :反過(guò)來(lái)(invert),只打印沒(méi)有匹配的,而匹配的反而不打印。

        -n :顯示行號(hào)

        -w :被匹配的文本只能是單詞,而不能是單詞中的某一部分,如文本中有l(wèi)iker,而我搜尋的只是like,就可以使用-w選項(xiàng)來(lái)避免匹配liker

        -c :顯示總共有多少行被匹配到了,而不是顯示被匹配到的內(nèi)容,注意如果同時(shí)使用-cv選項(xiàng)是顯示有多少行沒(méi)有被匹配到。

        --color :將匹配到的內(nèi)容以顏色高亮顯示。

        模式部分:

        1、直接輸入要匹配的字符串,這個(gè)可以用fgrep(fast grep)代替來(lái)提高查找速度,比如我要匹配一下hello.c文件中printf的個(gè)數(shù):grep -c "printf" hello.c

        2、使用基本正則表達(dá)式,下面談關(guān)于基本正則表達(dá)式的使用:

        匹配字符:

        . :任意一個(gè)字符。

        [abc] :表示匹配一個(gè)字符,這個(gè)字符必須是abc中的一個(gè)。

        [a-zA-Z] :表示匹配一個(gè)字符,這個(gè)字符必須是a-z或A-Z這52個(gè)字母中的一個(gè)。

        [^123] :匹配一個(gè)字符,這個(gè)字符是除了1、2、3以外的所有字符。

        對(duì)于一些常用的字符集,系統(tǒng)做了定義:

        [A-Za-z]等價(jià)于[[:alpha:]]

        [0-9]等價(jià)于[[:digit:]]

        [A-Za-z0-9]等價(jià)于[[:alnum:]]

        tab,space等空白字符[[:space:]]

        [A-Z]等價(jià)于[[:upper:]]

        [a-z]等價(jià)于[[:lower:]]

        標(biāo)點(diǎn)符號(hào)[[:punct:]]

        eg1:我想在hello.c文件中匹配printf但是要求其后面緊跟的不是數(shù)字

        grep "printf[^[:digit:]]" hello.c

        匹配次數(shù):

        \{m,n\} :匹配其前面出現(xiàn)的字符至少m次,至多n次。

        \? :匹配其前面出現(xiàn)的內(nèi)容0次或1次,等價(jià)于\{0,1\}。

        * :匹配其前面出現(xiàn)的內(nèi)容任意次,等價(jià)于\{0,\},所以 ".*" 表述任意字符任意次,即無(wú)論什么內(nèi)容全部匹配。

        eg2:我想在hello.c文件中匹配print和printf

        grep "printf\?" hello.c

        位置錨定:

        ^ :錨定行首

        $ :錨定行尾。技巧:"^$"用于匹配空白行。

        \b或\<:錨定單詞的詞首。如"\blike"不會(huì)匹配alike,但是會(huì)匹配liker

        \b或\>:錨定單詞的詞尾。如"\blike\b"不會(huì)匹配alike和liker,只會(huì)匹配like

        \B :與\b作用相反。

        eg3:我想在hello.c文件中匹配以 h 開(kāi)頭以 o 結(jié)尾的字符串。

        grep "\<h.*o\>" hello.c

        eg4:我想在hello.c中匹配行首為數(shù)字,行尾為字母的行

        grep "^[[:digit:]].*[[:alpha:]]$" hello.c

        分組及引用:

        \(string\) :將string作為一個(gè)整體方便后面引用

         class="main">

      linux中的grep命令的用法詳解

      時(shí)間: 佳洲1085 分享

        

        linxu下的grep命令是經(jīng)常使用到的命令之一。下面由學(xué)習(xí)啦小編為大家整理了linux的grep命令的用法詳解的相關(guān)知識(shí),希望對(duì)大家有幫助!

        一、linux中的grep命令的用法詳解

        作為linux中最為常用的三大文本(awk,sed,grep)處理工具之一,掌握好其用法是很有必要的。

        首先談一下grep命令的常用格式為:【grep [選項(xiàng)] ”模式“ [文件]】

        常用選項(xiàng):

        -E :開(kāi)啟擴(kuò)展(Extend)的正則表達(dá)式。

        -i :忽略大小寫(xiě)(ignore case)。

        -v :反過(guò)來(lái)(invert),只打印沒(méi)有匹配的,而匹配的反而不打印。

        -n :顯示行號(hào)

        -w :被匹配的文本只能是單詞,而不能是單詞中的某一部分,如文本中有l(wèi)iker,而我搜尋的只是like,就可以使用-w選項(xiàng)來(lái)避免匹配liker

        -c :顯示總共有多少行被匹配到了,而不是顯示被匹配到的內(nèi)容,注意如果同時(shí)使用-cv選項(xiàng)是顯示有多少行沒(méi)有被匹配到。

        --color :將匹配到的內(nèi)容以顏色高亮顯示。

        模式部分:

        1、直接輸入要匹配的字符串,這個(gè)可以用fgrep(fast grep)代替來(lái)提高查找速度,比如我要匹配一下hello.c文件中printf的個(gè)數(shù):grep -c "printf" hello.c

        2、使用基本正則表達(dá)式,下面談關(guān)于基本正則表達(dá)式的使用:

        匹配字符:

        . :任意一個(gè)字符。

        [abc] :表示匹配一個(gè)字符,這個(gè)字符必須是abc中的一個(gè)。

        [a-zA-Z] :表示匹配一個(gè)字符,這個(gè)字符必須是a-z或A-Z這52個(gè)字母中的一個(gè)。

        [^123] :匹配一個(gè)字符,這個(gè)字符是除了1、2、3以外的所有字符。

        對(duì)于一些常用的字符集,系統(tǒng)做了定義:

        [A-Za-z]等價(jià)于[[:alpha:]]

        [0-9]等價(jià)于[[:digit:]]

        [A-Za-z0-9]等價(jià)于[[:alnum:]]

        tab,space等空白字符[[:space:]]

        [A-Z]等價(jià)于[[:upper:]]

        [a-z]等價(jià)于[[:lower:]]

        標(biāo)點(diǎn)符號(hào)[[:punct:]]

        eg1:我想在hello.c文件中匹配printf但是要求其后面緊跟的不是數(shù)字

        grep "printf[^[:digit:]]" hello.c

        匹配次數(shù):

        \{m,n\} :匹配其前面出現(xiàn)的字符至少m次,至多n次。

        \? :匹配其前面出現(xiàn)的內(nèi)容0次或1次,等價(jià)于\{0,1\}。

        * :匹配其前面出現(xiàn)的內(nèi)容任意次,等價(jià)于\{0,\},所以 ".*" 表述任意字符任意次,即無(wú)論什么內(nèi)容全部匹配。

        eg2:我想在hello.c文件中匹配print和printf

        grep "printf\?" hello.c

        位置錨定:

        ^ :錨定行首

        $ :錨定行尾。技巧:"^$"用于匹配空白行。

        \b或\<:錨定單詞的詞首。如"\blike"不會(huì)匹配alike,但是會(huì)匹配liker

        \b或\>:錨定單詞的詞尾。如"\blike\b"不會(huì)匹配alike和liker,只會(huì)匹配like

        \B :與\b作用相反。

        eg3:我想在hello.c文件中匹配以 h 開(kāi)頭以 o 結(jié)尾的字符串。

        grep "\<h.*o\>" hello.c

        eg4:我想在hello.c中匹配行首為數(shù)字,行尾為字母的行

        grep "^[[:digit:]].*[[:alpha:]]$" hello.c

        分組及引用:

        \(string\) :將string作為一個(gè)整體方便后面引用

        \1 :引用第一個(gè)左括號(hào)及其對(duì)應(yīng)的右括號(hào)所匹配的內(nèi)容。

        \2 :引用第二個(gè)左括號(hào)及其對(duì)應(yīng)的右括號(hào)所匹配的內(nèi)容。

        eg5:我想在hello.c文件中匹配行首以 l 開(kāi)頭 e 結(jié)尾的單詞(比如 like,love等),行尾以相同的單詞結(jié)尾。(比如這種行:large dog is a dog that is so large)

        grep "^\(l.*e\b\).*\b\1$" hello.c

        二、linux的grep命令的用法大全

        查找特定字符串并顏色顯示

        [root@ www.linuxidc.com]# grep -n 'the' regular_express.txt --color=auto

        8:I can't finish the test.

        12:the symbol '*' is represented as start.

        15:You are the best is mean you are the no. 1.

        16:The world <Happy> is the same with "glad".

        18:google is the best tools for search keyword.

        反向選擇查找特定字符串并顏色顯示

        [root@ www.linuxidc.com]# grep -vn 'the' regular_express.txt --color=auto

        1:"Open Source" is a good mechanism to develop programs.

        2:apple is my favorite food.

        ......

        忽略大小寫(xiě)查找特定的字符

        [root@ www.linuxidc.com]# grep -in 'the' regular_express.txt

        使用 [] 來(lái)查找集合字符:

        [root@ www.linuxidc.com]# grep -n 't[ae]st' regular_express.txt

        8:I can't finish the test.

        9:Oh! The soup taste good.

        查找有‘oo’字符串

        [root@ www.linuxidc.com]# grep -n 'oo' regular_express.txt

        1:"Open Source" is a good mechanism to develop programs.

        2:apple is my favorite food.

        3:Football game is not use feet only.

        9:Oh! The soup taste good.

        18:google is the best tools for search keyword.

        19:goooooogle yes!

        查找有‘oo’字符串,但是不要前面有g(shù)的,即剔除goo

        [root@ www.linuxidc.com]# grep -n '[^g]oo' regular_express.txt

        2:apple is my favorite food.

        3:Football game is not use feet only.

        18:google is the best tools for search keyword.

        19:goooooogle yes! :引用第二個(gè)左括號(hào)及其對(duì)應(yīng)的右括號(hào)所匹配的內(nèi)容。

        eg5:我想在hello.c文件中匹配行首以 l 開(kāi)頭 e 結(jié)尾的單詞(比如 like,love等),行尾以相同的單詞結(jié)尾。(比如這種行:large dog is a dog that is so large)

        grep "^\(l.*e\b\).*\b class="main">

      linux中的grep命令的用法詳解

      時(shí)間: 佳洲1085 分享

        二、linux的grep命令的用法大全

        查找特定字符串并顏色顯示

        [root@ www.linuxidc.com]# grep -n 'the' regular_express.txt --color=auto

        8:I can't finish the test.

        12:the symbol '*' is represented as start.

        15:You are the best is mean you are the no. 1.

        16:The world <Happy> is the same with "glad".

        18:google is the best tools for search keyword.

        反向選擇查找特定字符串并顏色顯示

        [root@ www.linuxidc.com]# grep -vn 'the' regular_express.txt --color=auto

        1:"Open Source" is a good mechanism to develop programs.

        2:apple is my favorite food.

        ......

        忽略大小寫(xiě)查找特定的字符

        [root@ www.linuxidc.com]# grep -in 'the' regular_express.txt

        使用 [] 來(lái)查找集合字符:

        [root@ www.linuxidc.com]# grep -n 't[ae]st' regular_express.txt

        8:I can't finish the test.

        9:Oh! The soup taste good.

        查找有‘oo’字符串

        [root@ www.linuxidc.com]# grep -n 'oo' regular_express.txt

        1:"Open Source" is a good mechanism to develop programs.

        2:apple is my favorite food.

        3:Football game is not use feet only.

        9:Oh! The soup taste good.

        18:google is the best tools for search keyword.

        19:goooooogle yes!

        查找有‘oo’字符串,但是不要前面有g(shù)的,即剔除goo

        [root@ www.linuxidc.com]# grep -n '[^g]oo' regular_express.txt

        2:apple is my favorite food.

        3:Football game is not use feet only.

        18:google is the best tools for search keyword.

        19:goooooogle yes!

      3635303