作為linux中最為常用的三大文本(awk,sed,grep)處理工具之一,掌握好其用法是很有必要的。
首先談一下grep命令的常用格式為:grep ?[選項(xiàng)] ?”模式“ ?[文件]
grep家族總共有三個(gè):grep,egrep,fgrep。
命令用法如下:
?查看grep命令的幫助信息:
grep --help
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 display version information and exit
--help display this help text and exit
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, --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
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
--group-separator=SEP use SEP as a group separator
--no-group-separator use empty string as a group separator
--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/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows)
'egrep' means 'grep -E'. 'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input. With no FILE, read . if a command-line
-r is given, - otherwise. If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
常用選項(xiàng):
-E :開(kāi)啟擴(kuò)展(Extend)的正則表達(dá)式。
-i :忽略大小寫(xiě)(ignore case)。
-v :反過(guò)來(lái)(invert),只打印沒(méi)有匹配的,而匹配的反而不打印。
-n :顯示行號(hào)
-w :被匹配的文本只能是單詞,而不能是單詞中的某一部分,如文本中有l(wèi)iker,而我搜尋的只是like,就可以使用-w選項(xiàng)來(lái)避免匹配liker
-c :顯示總共有多少行被匹配到了,而不是顯示被匹配到的內(nèi)容,注意如果同時(shí)使用-cv選項(xiàng)是顯示有多少行沒(méi)有被匹配到。
-o :只顯示被模式匹配到的字符串。
--color :將匹配到的內(nèi)容以顏色高亮顯示。
-A ?n:顯示匹配到的字符串所在的行及其后n行,after
-B ?n:顯示匹配到的字符串所在的行及其前n行,before
-C ?n:顯示匹配到的字符串所在的行及其前后各n行,context
模式部分:
1、直接輸入要匹配的字符串,這個(gè)可以用fgrep(fast grep)代替來(lái)提高查找速度,比如我要匹配一下hello.c文件中printf的個(gè)數(shù):fgrep ?-c ?"printf" ?hello.c
2、使用基本正則表達(dá)式,下面談關(guān)于基本正則表達(dá)式的使用:
匹配字符:
. :任意一個(gè)字符。
[abc] :表示匹配一個(gè)字符,這個(gè)字符必須是abc中的一個(gè)。
[a-zA-Z] :表示匹配一個(gè)字符,這個(gè)字符必須是a-z或A-Z這52個(gè)字母中的一個(gè)。
[^123] :匹配一個(gè)字符,這個(gè)字符是除了1、2、3以外的所有字符。
對(duì)于一些常用的字符集,系統(tǒng)做了定義:
[A-Za-z] 等價(jià)于 [[:alpha:]]
[0-9] 等價(jià)于 [[:digit:]]
[A-Za-z0-9] 等價(jià)于 [[:alnum:]]
tab,space 等空白字符 [[:space:]]
[A-Z] 等價(jià)于 [[:upper:]]
[a-z] 等價(jià)于 [[:lower:]]
標(biāo)點(diǎn)符號(hào) [[:punct:]]
匹配次數(shù):
\{m,n\} :匹配其前面出現(xiàn)的字符至少m次,至多n次。
\? :匹配其前面出現(xiàn)的內(nèi)容0次或1次,等價(jià)于\{0,1\}。
* :匹配其前面出現(xiàn)的內(nèi)容任意次,等價(jià)于\{0,\},所以 ".*" 表述任意字符任意次,即無(wú)論什么內(nèi)容全部匹配。
位置錨定:
^ :錨定行首
$ :錨定行尾。技巧:"^$"用于匹配空白行。
\b或\<:錨定單詞的詞首。如"\blike"不會(huì)匹配alike,但是會(huì)匹配liker
\b或\>:錨定單詞的詞尾。如"\blike\b"不會(huì)匹配alike和liker,只會(huì)匹配like
\B :與\b作用相反。
分組及引用:
\(string\) :將string作為一個(gè)整體方便后面引用
\1 :引用第1個(gè)左括號(hào)及其對(duì)應(yīng)的右括號(hào)所匹配的內(nèi)容。
\2 :引用第2個(gè)左括號(hào)及其對(duì)應(yīng)的右括號(hào)所匹配的內(nèi)容。
\n :引用第n個(gè)左括號(hào)及其對(duì)應(yīng)的右括號(hào)所匹配的內(nèi)容。
擴(kuò)展的(Extend)正則表達(dá)式(注意要使用擴(kuò)展的正則表達(dá)式要加-E選項(xiàng),或者直接使用egrep):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-542593.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-542593.html
到了這里,關(guān)于linux中g(shù)rep命令的常見(jiàn)用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!