歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux下grep命令用法實例教程

一,grep命令有什麼用

個人覺得Linux下grep命令就是一個對文本或輸出進行匹配並控制輸出的一個工具,看一下下面的參數,部分翻譯了,有不對的地方,還請指正

grep --help
匹配模式選擇:
 -E, --extended-regexp     擴展正則表達式egrep
 -F, --fixed-strings       一個換行符分隔的字符串的集合fgrep
 -G, --basic-regexp        基本正則
 -P, --perl-regexp         調用的perl正則
 -e, --regexp=PATTERN      後面根正則模式,默認無
 -f, --file=FILE           從文件中獲得匹配模式
 -i, --ignore-case         不區分大小寫
 -w, --word-regexp         匹配整個單詞
 -x, --line-regexp         匹配整行
 -z, --null-data           a data line ends in 0 byte, not newline

雜項:
 -s, --no-messages         不顯示錯誤信息
 -v, --invert-match        顯示不匹配的行
 -V, --version             顯示版本號
 --help                    顯示幫助信息
 --mmap                use memory-mapped input if possible

輸入控制:
 -m, --max-count=NUM       匹配的最大數
 -b, --byte-offset         打印匹配行前面打印該行所在的塊號碼。
 -n, --line-number         顯示的加上匹配所在的行號
 --line-buffered           刷新輸出每一行
 -H, --with-filename       當搜索多個文件時,顯示匹配文件名前綴
 -h, --no-filename         當搜索多個文件時,不顯示匹配文件名前綴
 --label=LABEL            print LABEL as filename for standard input
 -o, --only-matching       show only the part of a line matching PATTERN
 -q, --quiet, --silent     不顯示任何東西
 --binary-files=TYPE   assume that binary files are TYPE
 TYPE is 'binary', 'text', or 'without-match'
 -a, --text                匹配二進制的東西
 -I                        不匹配二進制的東西
 -d, --directories=ACTION  目錄操作,讀取,遞歸,跳過
 ACTION is 'read', 'recurse', or 'skip'
 -D, --devices=ACTION      設置對設備,FIFO,管道的操作,讀取,跳過
 ACTION is 'read' or 'skip'
 -R, -r, --recursive       遞歸調用
 --include=PATTERN     files that match PATTERN will be examined
 --exclude=PATTERN     files that match PATTERN will be skipped.
 --exclude-from=FILE   files that match PATTERN in FILE will be skipped.
 -L, --files-without-match 匹配多個文件時,顯示不匹配的文件名
 -l, --files-with-matches  匹配多個文件時,顯示匹配的文件名
 -c, --count               顯示匹配了多少次
 -Z, --null                print 0 byte after FILE name

文件控制:
 -B, --before-context=NUM  打印匹配本身以及前面的幾個行由NUM控制
 -A, --after-context=NUM   打印匹配本身以及隨後的幾個行由NUM控制
 -C, --context=NUM         打印匹配本身以及隨後,前面的幾個行由NUM控制
 -NUM                      根-C的用法一樣的
 --color[=WHEN],
 --colour[=WHEN]       use markers to distinguish the matching string
 WHEN may be `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)

Copyright © Linux教程網 All Rights Reserved