grep is often used when querying messages on the server, summarizing several common, simple commands, and mastering them to make queries more efficient.
Basic format:
grep [option] Pattern file
Several important command parameters, which are not listed here, only list the ones that may be commonly used for quick query:
In addition to any parameters that indicate that the search conforms to the content of the style after, the remaining parameters are:
-v--revert- match -i--ignore-case #忽略字符大小写的差别. - n--line- number #在显示符合样式的那一行之前, indicating the column number of the row. -C--count #计算符合样式的列数.
Example:
[Email protected] yyy]$ grep-c'MONGO'Nohup. outGrep:nohup. out: Permission denied[[email protected] yyy]$ sudo grep-C'MONGO'Nohup. out 10633[[email protected] yyy]$ sudo grep-V' not'stop.sh #!/bin/shprocess= ' Ps-ef|grep zzz|grep-v grep|grep-v Ppid|awk'{print $}'' num= ' Ps-ef|grep zzz|grep-v GREP|WC-l ' forIinch$PROCESS DoEcho"Kill the consumer process [$i]"Kill-9$idoneecho"$num processes be killed"[[email protected] yyy]$ sudo grep-i ppid stop.sh PROCESS= ' Ps-ef|grep zzz|grep-v grep|grep-v Ppid|awk'{print $}'' [[email protected] yyy]$ sudo grep-N-I ppid stop.sh2:P rocess= ' Ps-ef|grep zzz|grep-v grep|grep-v Ppid|awk'{print $}'`
If you combine grep with a regular expression, you can make the tool more powerful, documenting several common regular expressions that you can use with the grep parameter:
\< #锚定单词的开始 such as:'\<grep' matches lines that contain words that begin with grep. \> #锚定单词的结束 like 'grep\>' matches a line that contains a word that ends with grep ^ #锚定行的开始 such as:'^grep' matches all lines that begin with grep. $ #锚定行的结束 such as:'grep$' matches all lines that end with grep.
[] #匹配一个指定范围内的字符
such as ' [Gg]rep ' matches grep and GRE
Example:
[email protected] yyy]$ sudo grep-n'\<grep'stop.sh2:P rocess= ' Ps-ef|grep zzz|grep-v grep|grep-v Ppid|awk'{print $}'`3: num= ' Ps-ef|grep zzz|grep-v GREP|WC-l ' [[email protected] yyy]$ sudo grep-N'^e'stop.sh9: Echo"$num processes be killed"[[email protected] yyy]$ sudo grep-N'i$'stop.sh7: Kill-9$i [[email protected] yyy]$ sudo grep-N [gg]rep stop.sh2:P rocess= ' Ps-ef|grep zzz|grep-v grep|grep-v Ppid|awk'{print $}'`3: num= ' ps-ef|grep zzz|grep-v grep|wc-l '
Finally, if you need to output the contents of the query to text, you can:
[email protected] yyy]$ lltotal59976-rwxrwxrwx1Root root0Oct + -: - 1. TXTDRWXR-xr-x2Root root4096May to -: $Lib-rw-r--r--1Root root39936Jul - the: -Zzz.jar-rw-r--r--1Root root39792June3 -: -Zzz.jar.20160714-rw-r--r--1Root root39835Jul - the: toZzz.jar.20160718-RW-------1Root root61218369Oct + -: -Nohup. out-rw-r--r--1Root root275June1 the: tostop.sh[[email protected] yyy]$ sudo grep'MONGO'Nohup. out>1. txt [[email protected] yyy]$ lltotal61184-rwxrwxrwx1Root root1159937Oct + -: - 1. TXTDRWXR-xr-x2Root root4096May to -: $Lib-rw-r--r--1Root root39936Jul - the: -Zzz.jar-rw-r--r--1Root root39792June3 -: -Zzz.jar.20160714-rw-r--r--1Root root39835Jul - the: toZzz.jar.20160718-RW-------1Root root61286858Oct + -: -Nohup. out-rw-r--r--1Root root275June1 the: tostop.sh
Linux Search Filter--grep