Linux常用命令grep:文件文本重复搜索及多文件多文本查询实战

在使用 Linux 系统日常操作中,面对繁杂的文件和文本,迅速找到所需信息往往让人感到困扰。然而,grep 命令犹如一把开启宝藏的“钥匙”,能有效帮助我们攻克这一难题。接下来,我们就来领略它的独特魅力。

grep 官方简介

Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c
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  safter 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.

图片[1]-Linux常用命令grep:文件文本重复搜索及多文件多文本查询实战-东山笔记

-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. Report bugs to: bug-grep@gnu.org GNU Grep home page: General help using GNU software: http://www.gnu.org/gethelp/

grep 是 Linux 系统中常用的命令,同样也是 Unix 工具之一,其主要功能是对文件和文本进行重复的搜索操作。一旦设定了特定的搜索条件,它便能高效地搜索文件及其内容,帮助我们获取所需的信息。我平时只是用它来简单地查看用户数据、校对数据。然而,最近在分析后台日志时,我才发现它的强大功能!

后台日志处理实践

服务器后台的日志数据量庞大,若直接从服务器提取,既费时又耗带宽。因此,我们采用了grep命令进行关键字筛选,并将结果重定向到新文件,这样一来,原本14G的日志文件便缩减至12M,使得后续的数据清洗和分析工作变得更为简便。这种高效处理日志的方法,在日常工作实际应用中,显著省了时间和资源。

多文件多种文本查询

egrep命令能够执行扩展的正则表达式搜索功能。在处理多个文件中的文本查询时,若查询条件为“或”,即OR关系,我们可以轻松地提取包含关键字wordA或wordB的文件内容。若查询条件为“与”,即AND关系,则需要使用管道符“|”进行连接,尽管没有现成的运算符,但依然能够实现搜索需求。

完全匹配关键词

grep 'wordA|wordB' *.py
grep -E 'wordA|wordB' *.doc
grep -e wordA -e wordB *.py
egrep "wordA|wordB" *.c

使用“-w”选项可以确保关键词的完全匹配。当您在搜索过程中希望获取完全匹配、精确对应的关键词结果时,该选项能够有效防止出现包含关键词片段的其他无关内容,从而使搜索结果更加精确。例如,在查找特定的代码或专业术语时,这一功能尤为实用。

忽略大小写与高亮显示

使用“-i”选项可以忽略字母的大小写差异,无论关键词是全大写、全小写还是大小写混合,都能实现搜索。另外,“–color”选项则可以将匹配的内容以高亮形式呈现,使得在繁杂的文本中迅速找到目标,使得查找过程更加直观、高效。

grep -e pattern1 *.py |grep -e pattern2

递归查找

递归搜索功能可以在指定的目录以及其所有子目录中寻找符合要求的文件和文本内容。若项目包含众多层级的文件夹,递归搜索便能实现一次性对所有相关文件进行搜索,无需逐层进入子目录逐一查找,从而能够迅速定位所需信息。

grep -w 'warning|error|critical' /home/logs

在使用 grep 命令的过程中,你是否遇到过一些有趣或者比较麻烦的情况?如果你觉得这篇文章对你有帮助,请不要忘记给它点个赞,并且将它分享出去!

egrep -wi --color 'warning|error|critical' /home/logs

© 版权声明
THE END
喜欢就支持一下吧
分享