grep

发布时间: 更新时间: 总字数:1860 阅读时间:4m 作者: IP上海 分享 网址

grep, egrep, fgrep(Global Regular Expression Print) - print lines matching a pattern

SYNOPSIS

grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

Help

# grep --help
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开头的行。
  • $ 锚定行的结束 如:‘grep$‘匹配所有以grep结尾的行。
  • . 匹配一个非换行符的字符 如:‘gr.p’匹配gr后接一个任意字符,然后是p。
  • * 匹配零个或多个先前字符 如:’*grep’匹配所有一个或多个空格后紧跟grep的行。
  • .* 一起用代表任意字符。
  • [] 匹配一个指定范围内的字符,如’[Gg]rep’匹配Grep和grep。
  • [^] 匹配一个不在指定范围内的字符,如:’[^A-FH-Z]rep’匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。
  • \(..\) 标记匹配字符,如’(love)’,love被标记为1。
  • \< 锚定单词的开始,如:’<grep’匹配包含以grep开头的单词的行。
  • \> 锚定单词的结束,如’grep>‘匹配包含以grep结尾的单词的行。
  • x\{m\} 重复字符x,m次,如:‘0{5}‘匹配包含5个o的行。
  • x\{m,\} 重复字符x,至少m次,如:‘o{5,}‘匹配至少有5个o的行。
  • x\{m,n\} 重复字符x,至少m次,不多于n次,如:‘o{5,10}‘匹配5–10个o的行。
  • \w 匹配文字和数字字符,也就是[A-Za-z0-9],如:‘G\w*p’匹配以G后跟零个或多个文字或数字字符,然后是p。
  • \W \w的反置形式,匹配一个或多个非单词字符,如点号句号等。
  • \b 单词锁定符,如: ‘\bgrep\b’只匹配grep。

示例

# 匹配多个 OR 的关系,`\|` 必须转义
grep 'pattern1\|pattern2' file...

# -E | 不用转义
grep -E 'pattern1|pattern2' file...

# 忽略大小写
grep -i 'fatal\|error\|critical' /var/log/nginx/error.log

# 排除字符
grep -v 'fatal\|error\|critical' /var/log/nginx/error.log

# 查看 OpenStack 配置文件
grep '^[^#]' /etc/nova/nova.conf
grep '^[a-z]' /etc/filebeat/filebeat.yml
grep ERROR -rn *

示例

grep "^#" /etc/denyhosts.conf     # 输出以#开头的行内容
grep "^[^#]" /etc/denyhosts.conf  # 输出非#开头的行内容

说明

  • grep 查看匹配内容上下几行
# 打印匹配行的前后10行
$ grep -10 'error' nova.log
或
$ grep -C 10 'error' nova.log
或
$ grep -A 10 -B 10 'error' nova.log

# 打印匹配行的后10行
$ grep -A 10 'error' nova.log

# 打印匹配行的前10行
$ grep -B 10 'error' nova.log
  • 从文件内容查找匹配指定字符串的行
$ grep "被查找的字符串" 文件名

例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件

grep "something" */*.in
  • 从文件内容查找与正则表达式匹配的行
$ grep –e "正则表达式" 文件名
  • 查找时不区分大小写
$ grep –i "被查找的字符串" 文件名
  • 查找匹配的行数
$ grep -c "被查找的字符串" 文件名
  • 查找不匹配指定字符串的行
$ grep –v "被查找的字符串" 文件名
  • 从根目录开始查找所有扩展名为.log的文本文件,并找出包含"ERROR"的行
find / -type f -name "*.log" | xargs grep "ERROR"
  • 从当前目录开始查找所有扩展名为.in的文本文件,并找出包含"something"的行
find . -name "*.in" | xargs grep "something"

pgrep

pgrep 是一个命令行工具,用来按照给定的条件查找正在运行的程序的进程ID,它是 procps(或者 procps-ng )软件包的一部分,示例

pgrep sshd
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数