linux shell 腳本攻略學習18--grep命令詳解


grep(global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是unix/linux中用於文本搜索的大師級的工具。它能夠接受正則表達式和通配符

首先,輸入grep --help查看幫助信息:

amosli@amosli-pc:~$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             print version information and exit
      --help                display this help and exit
      --mmap                ignored for backwards compatibility

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the file name for each match
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is `binary', `text', or `without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is `read', `recurse', or `skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is `read' or `skip'
  -R, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is `always', `never', or `auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'.  `fgrep' means `grep -F'.
Direct invocation as either `egrep' or `fgrep' is deprecated.
With no FILE, or when FILE is -, read standard input.  If less than two FILEs
are given, assume -h.  Exit status is 0 if any line was selected, 1 otherwise;
if any error occurs and -q was not given, the exit status is 2.

語法格式:

     grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

參數-實例:

test.txt

amosli@amosli-pc:~/learn/grep$ cat test.txt 
hi,amos
this is linux world 
grep command is powerful!

1.在文件中搜索一個單詞

iamosli@amosli-pc:~/learn/grep$ grep 'hi' test.txt 
hi,amos
this is linux world 

2.從標准輸入中搜索一個單詞

amosli@amosli-pc:~/learn/grep$ echo this is a word  | grep hi
this is a word

也可以加上參數-e,表示根據模式進行匹配,下面部分也將會對-e參數進行介紹

amosli@amosli-pc:~/learn/grep$ echo  this is a word  | grep -e hi
this is a word

3.-E參數,使用正則表達式進行搜索關鍵字

amosli@amosli-pc:~/learn/grep$ echo  this is a word  | grep -E [a-z]+d
this is a word

-E參數英文提示信息:

  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)

意思是使用擴展(extended)的正則表達式.在文章開始的提示信息中有這么一段"egrep' means `grep -E'. ",所以也可以直接使用egrep命令進行搜索

amosli@amosli-pc:~/learn/grep$ echo  this is a word  | egrep "[a-z]+d"
this is a word

4.-o參數,只輸出匹配到的文本部分,沒有匹配到的不顯示

amosli@amosli-pc:~/learn/grep$ echo this a line. | grep -o -E "[a-z]+\."
line.
#或者
amosli@amosli
-pc:~/learn/grep$ echo this a line. | egrep -o "[a-z]+\." line.

英文提示信息:

-o, --only-matching       show only the part of a line matching PATTERN

5.--color參數,重點標記出匹配到的文本

amosli@amosli-pc:~/learn/grep$ echo this a line. | egrep -o "[a-z]+\." --color=auto
line.

除了auto之外,還可以選擇never和always,這個是表示什么時候重點標記。

英文提示信息:

  --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is `always', `never', or `auto'

6.-v參數,打印除了匹配到的文本以外的內容

常用格式:

grep -v match_pattern file

例:

amosli@amosli-pc:~/learn/grep$ grep -v 'hi,amos' test.txt 
this is linux world 
grep command is powerful!

英文提示信息:

  -v, --invert-match        select non-matching lines

7.-c參數,統計關鍵詞出現的行數

amosli@amosli-pc:~/learn/grep$ grep -c hi test.txt 
2

英文提示信息:

  -c, --count               print only a count of matching lines per FILE

這里grep只是統計出現的行數,而不是統計關鍵詞出現的次數

如:

amosli@amosli-pc:~/learn/grep$ echo -e  "1 2 3\nhelo\n 4 5" | grep -c "[0-9]"
2

這里有5個數字,但只有3行,其中有兩行中出現數字,所以顯示的次數為2

如何統計文件中關鍵詞出現的次數?

可以使用wc 命令進行二次統計,如下:

amosli@amosli-pc:~/learn/grep$ echo -e  "1 2 3\nhelo\n 4 5" | grep "[0-9]" | wc -w
5

8.-n參數,打印關鍵詞所在行數

amosli@amosli-pc:~/learn/grep$ grep  -n 'hi' test.txt 
1:hi,amos
2:this is linux world 

在第1行顯示了關鍵詞所在的行,極大的方便了閱讀。

英文提示信息:

  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line

9.對多個文件進行搜索

test2.txt

amosli@amosli-pc:~/learn/grep$ cat test2.txt 
hi,amos
this is linux world 
Welcome you!

對test.txt 和test2.txt進行批量查詢:

amosli@amosli-pc:~/learn/grep$ grep -n 'linux' test.txt test2.txt 
test.txt:2:this is linux world 
test2.txt:2:this is linux world 

10.-l參數,對多個文件進行搜索,但只顯示文件名

amosli@amosli-pc:~/learn/grep$ grep -l 'linux' test.txt test2.txt 
test.txt
test2.txt

英文提示信息: 

  -l, --files-with-matches  print only names of FILEs containing matches

  11.-i 參數,忽略大小寫進行搜索

amosli@amosli-pc:~/learn/grep$ echo "THIS IS test" | grep -i "th" THIS IS test

英文提示信息: 

  -i, --ignore-case         ignore case distinctions

12.-R參數,遞歸進行搜索

amosli@amosli-pc:~/learn/grep$ cd ..
amosli@amosli-pc:~/learn$ grep 'hi,amos' . -R
./grep/test.txt:hi,amos
./grep/test2.txt:hi,amos

英文提示信息: 

  -R, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.

13.-e參數,用grep匹配多個樣式,即多個條件進行查詢

amosli@amosli-pc:~/learn/grep$ grep -e "hi" -e "linux"  -e "grep" test.txt 
hi,amos
this is linux world 
grep command is powerful!

英文提示信息:

  -e, --regexp=PATTERN      use PATTERN for matching

還有一種方法可以進行樣式匹配,即把所要匹配的關鍵詞用一個文件保存起來,然后進行檢索,請看下一個參數-f

14.-f參數,用grep匹配多個樣式,即多個條件進行查詢--方法2

將關鍵詞保存到f.txt中:

amosli@amosli-pc:~/learn/grep$ cat  f.txt
hi
linux
grep

開始進行匹配:

amosli@amosli-pc:~/learn/grep$ grep -f f.txt test.txt 
hi,amos
this is linux world 
grep command is powerful!

結果跟-e參數一樣,但在大文件多關鍵詞的條件下,-f參數絕對提高了查詢的效率。

英文提示信息:

  -f, --file=FILE           obtain PATTERN from FILE

 15.在grep搜索中包括或排除指定的文件(--include參數和--exclude參數)

amosli@amosli-pc:~/learn/grep$ cat test.html 
hi,amos
this is linux world 
grep command is powerful!

開始檢索只包括.html的文件:

amosli@amosli-pc:~/learn/grep$ grep "hi,amos" . -r --include *.html -n
./test.html:1:hi,amos

開始檢索不包括.html的文件:

amosli@amosli-pc:~/learn/grep$ grep "hi,amos" . -r --exclude *.html -n
./test.txt:1:hi,amos
./test2.txt:1:hi,amos

英文提示信息:

 -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.

 16.-A,-B,-C ,打印出關鍵詞前、后的行

amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '3'
3

-A,打印出關鍵詞之后的5行,after

amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '3' -A 5
3
4
5
6
7
8

-B,打印出關鍵詞之前的2行,before

amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '3' -B 2
1
2
3

-C,打印出關鍵詞前后2行

amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '3' -C 2
1
2
3
4
5

英文提示信息:

  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context

17.-q 參數,靜默模式輸出

在靜默模式中,grep 命令不會向標准輸出打印任何輸出,它僅是運行命令,然后根據命令執行成功與否返回退出狀態,沒有找到值則返回狀態為1,找到則返回0.

沒有找到關鍵詞:

amosli@amosli-pc:~/learn/grep$ grep -q 'abc' test.txt 
amosli@amosli-pc:~/learn/grep$ $?
1: command not found

找到關鍵詞:

amosli@amosli-pc:~/learn/grep$ grep -q 'amos' test.txt 
amosli@amosli-pc:~/learn/grep$ $?
0: command not found

英文提示信息:

  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is `binary', `text', or `without-match'

18、其他參數,可輸入man grep查看grep命令手冊

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM