文本三劍客之grep

grep

grep 是最常用的文本搜索工具,即 globally search a regulare expression and print的縮寫詞,它能使用特定模式匹配(包括正則表達式)搜索文本,並默認輸出匹配行。Unix的grep家族包括grep、egrep和fgrep。

grep最主要的用途是文本過濾,用於過濾與模式匹配的行,這個模式可以是給定的字符串或者正則表達式。可以在文件、標準輸入或者標註輸出處過濾。

使用文檔如下

grep [OPTION]... PATTERN [FILE]...

  -E, --extended-regexp 該模式是擴展後的正則表達式,使用該參數後和egrep效果相同,與egrep等價
  -F, --fixed-strings   該模式是一組用換行符分隔的固定字符串,不使用正則匹配,直接匹配,與fgrep等價
  -G, --basic-regexp    該模式基本的正則表達式
  -P, --perl-regexp     該模式是Perl類型的正則表達式
  -e, --regexp=PATTERN  使用正則模式來匹配
  -f, --file=FILE       從文件FILE中尋找模式作爲匹配的項
  -i, --ignore-case     忽略大小寫
  -w, --word-regexp     強制模式只匹配整個單詞,如果模式是待匹配的字符串的一部分,則匹配失敗
  -x, --line-regexp     強制模式只能匹配整行
  -z, --null-data       匹配數據行以0字節結尾的行,而不是換行符

其他選項:
  -s, --no-messages     禁止顯示錯誤信息
  -v, --invert-match    選擇與模式不匹配的行,反選
  -V, --version         打印grep的版本信息並退出
      --help            打印幫助信息並退出

輸出控制:
  -m, --max-count=NUM   最多顯示NUM條匹配的行
  -b, --byte-offset     打印匹配行在文本中的字節位移數 
  -n, --line-number     輸出匹配行在文本中所在行的行號
      --line-buffered   沖掉每一行的輸出
  -H, --with-filename   打印每一個匹配行所在的文件
  -h, --no-filename     不顯示輸出行所在文件中的文件名前綴 
      --label=LABEL     用LABEL作爲標準輸入文件名前綴
  -o, --only-matching   只顯示每行與模式匹配的部分
  -q, --quiet, --silent 不顯示正常的輸出
    --binary-files=TYPE 假設二進制文件類型是'binary', 'text', 或者 'without-match'
  -a, --text            等效於--binary-files=text
  -I                    等效於--binary-files=without-match
  -d, --directories=ACTION  以'read', 'recurse', 或者 'skip'的方式來處理文件夾
  -D, --devices=ACTION  以'read' 或者 'skip'的方式來處理devices, FIFOs and sockets
  -r, --recursive       類似--directories=recurse選項
  -R, --dereference-recursive
                   取消引用遞歸
      --include=FILE_PATTERN
                   僅僅搜索與FILE_PATTERN匹配的文件
      --exclude=FILE_PATTERN
                   搜索與FILE_PATTERN不匹配的文件和目錄
      --exclude-from=FILE   
                   跳過與FILE中的任何文件模式匹配的文件
      --exclude-dir=PATTERN 
                   與模式匹配的目錄將被跳過
  -L, --files-without-match 只打印不匹配文件的名稱
  -l, --files-with-matches  僅打印包含匹配項的文件名
  -c, --count               打印與模式匹配的總行數
 
上下文控制選項:
  -B, --before-context=NUM  打印匹配項前NUM行內容
  -A, --after-context=NUM   打印匹配項後NUM行內容
  -C, --context=NUM         打印與模式匹配的前後NUM行內容
  -NUM                      與選項--context=NUM相同
      --group-separator=SEP 使用SEP作爲組分隔符
      --no-group-separator  使用空串作爲組分隔符
      --color[=WHEN],
      --colour[=WHEN]  使用標記高亮突出匹配的字符串,WHEN可取的值有:'always', 'never', 或者 'auto'
  -U, --binary       不剝離行尾的CR字符串
  • 用途

Search for PATTERN in each FILE or standard input.即從每個文件或者標準輸入中搜索滿足給定的模式,可以用兩種基本的方式來使用:

# 方式1直接從1到多個文件中搜索滿足條件的文本
grep patten file1 file2 ...

#方式2 從標準輸入中(通常用的最多的是管道)搜索滿足條件的文本
cat test.txt |grep pattern
  • 參數說明及舉例

添加一個名爲addcontent.txt的文件並瀏覽

[root@localhost testshell]# touch addcontent.txt && echo "old content" > addcontent.txt && cat addcontent.txt 
old content
total 8
drwxr-xr-x.  4 root root   47 Sep 12 11:50 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
-rw-r--r--.  1 root root   12 Sep 12 11:51 addcontent.txt
drwxr-xr-x.  2 root root    6 Sep 12 11:30 awk
drwxr-xr-x.  2 root root    6 Sep 12 11:30 tee

-E, --extended-regexp 擴展正則表達式

[root@localhost testshell]# grep -E "[-]+" addcontent.txt 

在這裏插入圖片描述
-F, --fixed-strings 把文本按照-F後面的字符切分

grep -F ":" addcontent.txt

``在這裏插入圖片描述
-G, --basic-regexp 系統默認支持
-P, --perl-regexp 顯式指定perl正則表達式支持
-e, --regexp=PATTERN 顯式指定正則表達式支持,支持多選項

grep -e "txt" -e "content" addcontent.txt

在這裏插入圖片描述
-f, --file=FILE 指定匹配文件中的正則表達式
添加一個regp.dat的文件,並追加content到文件中

echo "content" > regp.dat 
grep -f regp.dat addcontent.txt

在這裏插入圖片描述
-i, --ignore-case 忽略匹配的字符的大小寫

grep -i "content" addcontent.txt

在這裏插入圖片描述
-w, --word-regexp 強制要求匹配整個模式

grep -w "content" addcontent.txt

在這裏插入圖片描述
-x, --line-regexp 強制模式匹配整行

grep -x "old content" addcontent.txt

在這裏插入圖片描述
-z, --null-data 數據行以0字節結尾,而不是換行符
-s, --no-messages 不顯示錯誤信息
-v, --invert-match 忽略匹配項,選擇不匹配的內容

grep -v "content" addcontent.txt

在這裏插入圖片描述
-V, --version 顯式版本信息
--help 顯式幫助文檔

  • 輸出控制參數說明及舉例(未完待續。。。)

    -m, --max-count=NUM
    -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, --recursive like --directories=recurse
    -R, --dereference-recursive
    likewise, but follow all symlinks
    --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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章