日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

15个实用的grep示例

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 15个实用的grep示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

首先,新建如下文件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.

1 在單個文件中搜索指定字符串

語法: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

$cp demo_file demo_file1 $grep "this" demo_*#結(jié)果如下 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忽略大小寫

語法:grep -i "string" FILE

$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 匹配正則表達式

語法:grep "REGEX" filename

$ grep "lines.*empty" demo_file Two lines above this line is empty.

補充一點正則:

? ? ?前面的字段出現(xiàn)0或1次

* ? ?前面的字段出現(xiàn)0或多次

+ ? ?前面的字段出現(xiàn)1或多次

{n} ?前面的字段出現(xiàn)n次

{n,} ?前面的字段出現(xiàn)n或者更多次

{,m} ?前面的字段最多出現(xiàn)m次

{n,m} 前面的字段至少出現(xiàn)n次,最多出現(xiàn)m次

5 完整匹配,忽略字串

語法:grep -w "word" FILENAME

$grep -i "is" demo_file #除匹配is之外,還會匹配his this等 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. 為防止這個現(xiàn)象,用-w

$ grep -iw "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. Two lines above this line is empty. And this is the last line. 6 查看匹配行的前、后和周圍(after/behind/around)

先新建demo_text

4. Vim Word NavigationYou 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行

語法:grep -A <N> "string" FILENAME

$ grep -A 3 -i "example" demo_text Example to show the difference between WORD and word* 192.168.1.1 - single WORD * 192.168.1.1 - seven words. 6.2 顯示前N行

語法:grep -B <N> "string" FILENAME

$ grep -B 2 "single WORD" demo_text Example to show the difference between WORD and word* 192.168.1.1 - single WORD 6.3 顯示周圍的行

直接上例子

$ 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 高亮顯示

export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32' 搜索關(guān)鍵字,匹配之后會顯示綠色。

如果不想高亮顯示,把auto改成none即可。

8 遞歸查找

語法:grep -r "string" .

會遍歷指定目錄下所有文件及子目錄下所有文件。

9 用-v顯示不匹配的行

語法:grep -v "string" FILENAME

下面的例子會匹配所有不包含go的行。

$grep -v "go" demo_txt 4. Vim Word NavigationYou 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 顯示不匹配所有規(guī)則的行

$ cat test-file.txt a b c d$ grep -v -e "a" -e "b" -e "c" test-file.txt d 11 計算匹配行數(shù)

語法:grep -c "pattern" filename

和-v連用就是計算不匹配的行數(shù)

12 只顯示匹配的文件名

語法:grep -l "pattern" file*

注意:-l是小寫的L

$ grep -l this demo_* demo_file demo_file1 13 只顯示匹配的字符串(使用正則的時候特別有用)

$ grep -o "is.*line" demo_file is line is the 1st lower case line is line is is the last line 14 顯示匹配的位置

$ cat temp-file.txt 12345 12345$ grep -o -b "3" temp-file.txt 2:3 8:3 15 顯示行號

$ 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.

轉(zhuǎn)載于:https://my.oschina.net/csensix/blog/134846

總結(jié)

以上是生活随笔為你收集整理的15个实用的grep示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。