grep(egrep fgrep)

Description:

print lines matching a pattern

Some ragular options:

--color=auto: show matched color

-v: show not matching pattern (hide the matched patterns)

-i: ignore upper and downer

-n: show line numbers

-c: show matched line numbers

-o: only show matched chars

-q: show nothing

-A#: after # lines (# is a number )

-B#: befaore # lines

-C#: -AB

-e: matched or not matched grep -e test1 -e test2

-w: match a full words

-f FILE: obtain patterns from FILE

-E: use Extended REGEXP

REGEXP(regular expression)

Description:

determines whether the regular expression exp matches part or all of string and returns 1 if it does, 0 if it does not, unless inline is specfied.

Support:

grep sed awk vim less nginx varnish and so on

Two types:

  • Basic REGEXP

  • Extended REGEXP (need ` grep -E`)

Help:

man 7 regex

Some REGEXP chars:

. match any chars (if . in [], the . means itself instead of any chars) .

[] match a char included in [].

[^] match chars not included in [].

[:alnum:] alpha and number.

[:alpha:] means any alpha like a-z,A-Z.

[:lower:] lower alpha.

[:upper:] upper alpha.

[:blank:] Space and Tab.

[:space:] its widther than [:blank:].

  • eg: grep -v "^[[:space:]]"*$ means filter the blank rows.

[:cntrl:] not print control chars.

[:digit:] decimal numbers. [:xdigit:] hexadecimal numbers.

[:graph:] printable char whit un-blank.

[:print:] printable chars.

[:punct:] punctuation.


* a char befaore * appears nothing, once or repeatedly. x >= 0 .

+ a char before + appears once or repeatedly. x > 0 .

? a char before ? appears nothing or once. 0 <= x <= 1 .

{#} # is a number. a char before {#} appears #. x = # .

{a,b} ==> a <= x <= b.

{a,} ==> x >= a.

{,b} ==> 0 <= x <= b.


^ match the header ^char.

  • eg: grep -v "^$" means hide the blank rows.

$ match the tail char$ .

< or \bCHAR prefix .

> or CHAR\b suffix .

() make this chars become a overall.

  • eg: echo "rootxxxrooe" | grep '(root).*\1' equivalent to grep '(root).*(root)'

| OR

  • eg: grep "^a|^b" means match the char whose header is a or b.

  • eg: a|b ==> a or b ; C|cat ==> C or cat ; (C|c)at ==> Cat or cat