【文本處理命令】之grep搜索命令詳解

1、grep搜索命令

  在平常使用中grep命令也是會常常用到的一個搜索命令。grep命令用於在文本中執行關鍵詞搜索,並顯示匹配的結果。html

格式:

grep [選項] [文件]

 Usage: grep [OPTION]... PATTERN [FILE]...正則表達式

經常使用選項:

-b,--byte-offset 將可執行文件binary看成文本文件來搜索 -c,--count 僅顯示找到的行數 -i , --ignore-case 忽略大小寫 -n,--line-number 顯示行號 -v, --revert-match    取反,列出沒有「關鍵詞」的行
-w, --word-regex  按單詞搜索,僅匹配這個字符串
-r  逐層便利目錄查看
--color  匹配到的行高亮顯示
--include  指定匹配的文件類型
--exinclude 過濾掉不須要匹配的文件類型
-A: 顯示匹配行及後面多少行, 如: -A3, 則表示顯示匹配行及後3行
-B: 顯示匹配行及前面多少行, 如: -B3, 則表示顯示匹配行及前3行
-C: 顯示匹配行先後多少行,   如: -C3, 則表示顯示批量行先後3行

正則匹配:

^  #行的開始 如:'^grep'匹配全部以grep開頭的行。 $ #行的結束 如:'grep$'匹配全部以grep結尾的行。 . #匹配一個非換行符的字符 如:'gr.p'匹配gr後接一個任意字符,而後是p。 *  #匹配零個或多個先前字符 如:'*grep'匹配全部一個或多個空格後緊跟grep的行。 .* #一塊兒用表明任意字符。

 ‘\?‘:匹配其前面的字符0次或者1次;shell

 ‘\+’:匹配其前面的字符1次或者屢次;centos

[] #匹配一個指定範圍內的字符,如'[Gg]rep'匹配Grep和grep。 [^]  #匹配一個不在指定範圍內的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。 \(..\) #標記匹配字符,如'\(love\)',love被標記爲1。 \<      #錨定單詞的開始,如:'\<grep'匹配包含以grep開頭的單詞的行。 \>      #錨定單詞的結束,如'grep\>'匹配包含以grep結尾的單詞的行。 x\{m\} #重複字符x,m次,如:'0\{5\}'匹配包含5個o的行。 x\{m,\} #重複字符x,至少m次,如:'o\{5,\}'匹配至少有5個o的行。 x\{m,n\} #重複字符x,至少m次,很少於n次,如:'o\{5,10\}'匹配5--10個o的行。 \w #匹配文字和數字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G後跟零個或多個文字或數字字符,而後是p。 \W #\w的反置形式,匹配一個或多個非單詞字符,如點號句號等。 \b #單詞鎖定符,如: '\bgrep\b'只匹配grep。

實例:

1)查詢當前系統中不容許登陸的用戶信息tomcat

[root@VM_0_10_centos shellScript]# grep /sbin/nologin /tmp/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin 或 [root@VM_0_10_centos shellScript]# cat /tmp/passwd  | grep /sbin/nologin

2)多文件查詢bash

# 查看多個文件匹配包含字母a的行 [root@VM_0_10_centos shellScript]# grep a test.sh test.txt test.sh:#!/bin/bash test.sh:echo "argument: $0"; test.txt:2 this is a test

 

3)查看既包含a又包含o的行app

[root@VM_0_10_centos shellScript]# grep a test.txt | grep o  [root@VM_0_10_centos shellScript]# cat test.txt | grep a | grep o 3 Are you like awk 10 There are orange,apple,mongo

 

 4)查找匹配a或者匹配o的行this

[root@VM_0_10_centos shellScript]# grep -e a -e o test.txt 或 [root@VM_0_10_centos shellScript]# cat test.txt | grep -e 'a' -e 'o'
2 this is a test 3 Are you like awk This's a test
10 There are orange,apple,mongo

 

5)匹配查詢內容的前n行,後n行,先後n行spa

#顯示匹配行前2行 grep a test.txt -A2 #顯示匹配行後2行 grep a test.txt -B2 #顯示匹配行先後2行 grep a test.txt -C2

PS:嘗試,可是結果仍是顯示的所有,不知道是我命令錯了仍是其餘緣由3d

6)匹配字符不區分大小寫

[root@VM_0_10_centos shellScript]# grep -i a test.txt 2 this is a test 3 Are you like awk This's a test
10 There are orange,apple,mongo

 

7)匹配正則表達式(匹配小寫a-z之間的5個字符,即包含5個小寫字母的字符)下面加粗部分顯示

[root@VM_0_10_centos shellScript]#  grep -e '[a-z]\{5\}' test.txt 10 There are orange,apple,mongo

 

8)統計包含a的行數

[root@VM_0_10_centos shellScript]# grep -c a test.txt 4

 

9)遍歷當前目錄及子目錄包含a的行

[root@VM_0_10_centos shellScript]# grep -r a . grep: memory exhausted [root@VM_0_10_centos shellScript]# grep -rI a . ./randowName.sh: clear ./cut.txt:abc:def:hij

PS:這裏不加-I會出現上面內存問題,這是由於grep -r查找的範圍會訪問全部這個目錄下的文件,包括二進制文件,加上-I參數不匹配查詢二進制文件,能夠解決這個問題。

 

10)遍歷當前目錄及全部子目錄,查找全部.txt類型的文件中包含a的字符

[root@VM_0_10_centos shellScript]# grep -rI a --include="*.txt" .
./cut.txt:abc:def:hij ./ed.txt:asdfghjkl

 

11)查找指定進程及其個數

 

PS:若是想值查詢tomcat進程,使用grep -v "grep"篩選便可( ps -ef | grep -v "grep" |grep "tomcat")

12)查找包含非「a」開頭的行

[root@VM_0_10_centos shellScript]#   grep ^[a] test.txt [root@VM_0_10_centos shellScript]#   grep ^[^a] test.txt 2 this is a test 3 Are you like awk This's a test 10 There are orange,apple,mongo

PS:grep可用於shell中。grep經過返回一個狀態值來講明搜索的狀態,結果{0:成功,1:不成功,2:搜索的文件不存在}

 

 

 

取出ip地址能夠查看我另外一篇博客:https://www.cnblogs.com/HeiDi-BoKe/p/11757961.html

相關文章
相關標籤/搜索