系统文件日志筛查总结--grep命令

grep

grep 作用: 搜索匹配模式的指定行信息。

用法: grep [选项]... PATTERN [FILE]...

英文过关且可以翻墙的可直接翻阅官方说明手册(共39页,不算大部头):http://www.gnu.org/software/grep/manual/grep.pdf

 

常用grep搜索

博主经验积累下的常用选项重点讲解,记住下面的,常规搜索就足够使用了(以下可合并使用):

-a :将 binary 文件以 text 文件的方式搜寻数据 如:grep命令执行后,显示匹配到二进制文件,而不显示具体内容,则加-a可以将binary文件,以text方式搜寻,就可以输出可见内容。
-e :匹配具体的Pattern
-w : 强制匹配具体的单词,如 grep -w wa 则wa显示,warn不显示。
-l : 输出包含匹配项目的文件名
-B : 后面加数字,代表before,不仅打印匹配的当前行,还打印指定行数的前几行,如 grep -B 5 打印匹配行与匹配行的前5行。
-A : 后面加数字,代表after,不仅打印匹配的当前行,还打印指定行数的后几行,如 grep -A 5 打印匹配行与匹配行的后5行。
-C : 后面加数字,代表context,不仅打印匹配的当前行,还打印指定行数的前后几行,如 grep -C 5 打印匹配行与匹配行的前后各5行。
-m : 后面加数字,代表maxcount,用于匹配数较多的情况,输出最多多少个匹配行。如 grep -m 5 最多打印5个匹配想。
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号,后续可通过行号,vi 文件名 然后输入行号,进行具体问题查询。
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
-r : 递归搜索,不仅搜索当前目录下的,也包括当前目录子目录下符合匹配的文件并输出
--include='搜索的文件名' :文件名匹配,如grep --include '*.java' 搜索符合匹配的java文件内容,并输出。同理--exclude='搜索的文件名'.
--color=auto :可以将找到的关键词部分加上颜色的显示

 

$ grep --help

使用 grep --help 可输出几乎所有grep用法的简明描述

以下为 grep --help 命令输出内容,红色为常用搜索选项

用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。

正则表达式选择与解释:
  -E, --extended-regexp     PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
  -F, --fixed-strings       PATTERN 是一组由断行符分隔的定长字符串。
  -G, --basic-regexp        PATTERN 是一个基本正则表达式(缩写为 BRE)
  -P, --perl-regexp         PATTERN 是一个 Perl 正则表达式
  -e, --regexp=PATTERN      用 PATTERN 来进行匹配操作 
  -f, --file=FILE           从 FILE 中取得 PATTERN
  -i, --ignore-case         忽略大小写
  -w, --word-regexp         强制 PATTERN 仅完全匹配字词
  -x, --line-regexp         强制 PATTERN 仅完全匹配一行
  -z, --null-data           一个 0 字节的数据行,但不是空行

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

输出控制:
  -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       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

文件控制:
  -B, --before-context=NUM  打印以文本起始的NUM 行
  -A, --after-context=NUM   打印以文本结尾的NUM 行
  -C, --context=NUM         打印输出文本NUM 行

  -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)

官方文件中关于grep的使用与解答学习:

以下的中文内容为博主本人理解后翻译,仅供参考。

1. How can I list just the names of matching files?

 怎样列出符合匹配的文件的文件名

  grep -l ’main’ *.c

  lists the names of all C files in the current directory whose contents mention ‘main’.

 列出当前目录下包含了main内容的c文件。

 

2. How do I search directories recursively?  

如何递归查询所有目录(即匹配目录与子目录下的文件)

grep -r ’hello’ /home/gigi

searches for ‘hello’ in all files under the /home/gigi directory.

查看 /home/gigi目录下的所有文件并输出内容(包括子目录下的文件),和1中的-l使用可只输出文件名。

 

3. What if a pattern has a leading ‘-’?  

如果pattern里有‘-’怎么办?

grep -e ’--cut here--’ *

searches for all lines matching ‘--cut here--’. Without -e, grep would attempt to parse ‘--cut here--’ as a list of options.

搜索包含 ‘--cut here--’的文件内容并输出,使用 -e , 否则包含‘-’的会被当做命令中的option选项解释。

 

4. Suppose I want to search for a whole word, not a part of a word? 

怎样搜索整个词汇,而不是词汇的部分?

grep -w ’hello’ *

searches only for instances of ‘hello’ that are entire words; it does not match ‘Othello’.

此命令搜索hello,不匹配‘Othello’.

 

5. How do I output context around the matching lines?  

怎样输出匹配行的上下文?

grep -C 2 ’hello’ *

prints two lines of context around each matching line.

输出匹配行的上下各两行。

 

6. How do I force grep to print the name of the file?

怎样在输出匹配内容的情况下,强制输出匹配内容所对应的文件名?

Append /dev/null:

后面加/dev/null

grep ’eli’ /etc/passwd /dev/null

Alternatively, use -H, which is a GNU extension:

grep -H ’eli’ /etc/passwd

或者使用 -H,GNU扩展的里的选项。

 

7. Why does grep report “Binary file matches”?

If grep listed all matching “lines” from a binary file, it would probably generate output that is not useful, and it might even muck up your display. So GNU grep suppresses output from files that appear to be binary files. To force GNU grep to output lines even from files that appear to be binary, use the -a or ‘--binary-files=text’ option. To eliminate the “Binary file matches” messages, use the -I or ‘--binary-files=without-match’ option.

如果grep输出二进制文件中的匹配行,不仅没什么用,可能还会很糟,想象一下整个屏幕都是二进制码。所以GNU grep 抑制了二进制文件的输出。如果想要输出正常的文本内容,使用 -a 或者‘--binary-files=text’ 选项。如果不想匹配二进制文件,可以使用 -I 或者 ‘--binary-files=without-match’

 

8. Why doesn’t ‘grep -lv’ print non-matching file names?

为什么 ‘grep -lv’ 不能输出不匹配的文件的文件名?

‘grep -lv’ lists the names of all files containing one or more lines that do not match. To list the names of all files that contain no matching lines, use the -L or --fileswithout-match option.

‘grep -lv’  列出的是有1到更多行不符合匹配的文件的文件名,如果需要列出完全不符合匹配的文件的文件名,使用 -L 或者 --fileswithout-match 选项。 

 

9. I can do “OR” with ‘|’, but what about “AND”?

或运算用‘|’来筛选,那么与运算呢?

grep ’paul’ /etc/motd | grep ’franc,ois’

finds all lines that contain both ‘paul’ and ‘franc,ois’.

如上grep两次即找出即包含‘paul’又包含 ‘franc,ois’的。

 

10. Why does the empty pattern match every input line?

怎样匹配文件中的空?

The grep command searches for lines that contain strings that match a pattern. Every line contains the empty string, so an empty pattern causes grep to find a match on each line. It is not the only such pattern: ‘^’, ‘$’, ‘.*’, and many other patterns cause grep to match every line. To match empty lines, use the pattern ‘^$’. To match blank lines, use the pattern ‘^[[:blank:]]*$’. To match no lines at all, use the command ‘grep -f /dev/null’.

匹配空行,使用模式 ‘^$’,匹配空白行‘^[[:blank:]]*$’,匹配什么数据也没有使用 ‘grep -f /dev/null’

 

11. How can I search in both standard input and in files?

怎样同时匹配标准输入与文件?

Use the special file name ‘-’:  使用 ‘-’

cat /etc/passwd | grep ’alain’ - /etc/motd

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值