当前位置: 移动技术网 > 网络运营>服务器>Linux > Linux常用命令之grep命令用法详解

Linux常用命令之grep命令用法详解

2020年08月17日  | 移动技术网网络运营  | 我要评论

1.官方简介

grep是linux的常用命令,用于对文件和文本执行重复搜索任务的unix工具,可以通过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  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.

report bugs to: bug-grep@gnu.org
gnu grep home page: <http://www.gnu.org/software/grep/>
general help using gnu software: http://www.gnu.org/gethelp/

我平时也是简单的查看一个用户数据,用于简单的数据校对,最近突然接到分析后台日志的需求,才发现grep用处还是不少的。

比如我们后台日志相当大,要是直接从服务器直接拉取,耗时长占用带宽,所以方案就是直接使用 grep关键字重定向到新的文件中,从14g直接到12m,然后再数据清洗和分析。

2.实战介绍

2.1使用grep命令对多文件中多种文本查询

note :使用egrep命令,可使用扩展的正则表达式

1.多文件

  • grep 'pattern' file1 file2

2.多文本 , 关系是or

  • egrep 'pattern1|pattern2' *.py
  • grep -e pattern1 -e pattern2 *.py
  • grep -e 'pattern1|pattern2' *.doc

例如下面对 对文件中 存在关键字 worda or wordb进行提取:

grep 'worda\|wordb' *.py
grep -e 'worda|wordb' *.doc
grep -e worda -e wordb *.py
egrep "worda|wordb" *.c

3.多文本关系是 and

这里我并没有看到 直接能用的【option】,只能加一层管道符|。

例如:

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

2.2完全匹配关键词 -w

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

2.3使用-i参数忽略大小写,–color高亮显示匹配结果

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

2.4递归查找

egrep -rwi --color 'warning|error' /home/logs/

到此这篇关于linux常用命令-grep命令用法详解的文章就介绍到这了,更多相关linux中grep命令详解内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网