grep-Global Regular Expressions Print

20 阅读2分钟

Tutorail: www.digitalocean.com/community/t…
Man page: man7.org/linux/man-p…
GNU Grep: www.gnu.org/software/gr…


man grep
grep "reg" filename

-r/-R 未指定文件时,递归的搜索指定文件夹下的所有文件


-r, --recursive
	Read  all  files  under each directory, recursively, following symbolic links only if they are on the command line.  Note that if no file operand is given, grep searches the working directory.  This is equivalent to the -d recurse option.
-R, --dereference-recursive
	Read all files under each directory, recursively.  Follow all symbolic links, unlike -r.

-i 不区分大小写

-i, --ignore-case
	gnore case distinctions in patterns and input data, so that characters that differ only in case match each other.

-c 计算匹配的行数

-c, --count
	Suppress normal output; instead print a count of **matching lines** for each input file.  With the -v, --invert-match  option
	(see above), count non-matching lines.

-v 打印不匹配的行(invert)

-v, --invert-match
	Invert the sense of matching, to select non-matching lines.

-n 打印行号

-n, --line-number
	  Prefix each line of output with the 1-based line number within its input file.

-w 精准匹配单词

-w, --word-regexp
	  Select only those lines containing matches that form whole words.  The test is that the matching substring must either be at  the  beginning of the line, or preceded by a non-word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word constituent character.  Word-constituent characters are letters,  digits,  and  the underscore.  This option has no effect if -x is also specified.

-A/-B/-C 打印上下文

       -A NUM, --after-context=NUM
              Print  NUM  lines  of  trailing  context  after  matching lines.  Places a line containing a group separator (--) between
              contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a  line  containing  a  group  separator  (--)  between
              contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

       -C NUM, -NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing a group separator (--) between contiguous groups of matches.
              With the -o or --only-matching option, this has no effect and a warning is given.

Regular Expressions​

 -E, --extended-regexp
  Interpret PATTERNS as extended regular expressions (EREs, see below).

-F, --fixed-strings
  Interpret PATTERNS as fixed strings, not regular expressions.

-G, --basic-regexp
  Interpret PATTERNS as basic regular expressions (BREs, see below).  This is the default.
  
-P, --perl-regexp
	  Interpret PATTERNS as Perl-compatible regular expressions (PCREs).  This option is experimental when combined with the -z (--null-data) option, and grep -P may warn of unimplemented features.

Extended Regular Expressions

  1. grouping
  2. alternation

注意特殊字符的转义