发布时间: 更新时间: 总字数:1336 阅读时间:3m 作者: 分享 复制网址

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


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


# 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

  -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
                            search only files that match 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
      --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

'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:
GNU Grep home page: <>
General help using GNU software: <>


  • ^ #锚定行的开始 如:’^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。


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