egrep is another mode of grep, which uses the-E option (to enable or close one or some functions of commands), to enable the extended regular expression engine function, and to check the line-by-line matching of text streams by using the combination of characters of extended regular expressions and plain text characters PATERN, to display the lines where the matched strings are located to standard output.
fgrep, grep-F option, turn off the function of regular expression engine, use pure text character combination as PATTERN, based on the unique algorithm, carry out efficient matching checking of text, and display the line where the matched string is located to the standard output.
egrep command
[root@izpo45bh60h6bsz ~]# type egrep egrep is aliased to `egrep --color=auto' [root@izpo45bh60h6bsz ~]# which --skip-alias egrep /usr/bin/egrep [root@izpo45bh60h6bsz ~]# egrep --help #Getting Use Help Usage: grep [OPTION]... PATTERN [FILE]... -E, --extended-regexp #ERE #Its help content is the same as the grep command
- E option, grep-E is equivalent to egrep
# grep -E '^root\b' /etc/passwd # egrep '^root\b' /etc/passwd
Extended regular expression characters
Character Matching
Matching times
Location anchoring
or
Character Matching
1). Any single character
2) [] Any single character within a specified range, [a b c] a or b or C
3) [^] Any single character outside the specified range
Use examples
[root@izpo45bh60h6bsz ~]# vim a.txt #vim is a text editing command. After entering, press the i key, then you can write the text. Press the esc key, then press shift plus: key, enter wq plus Enter key. how are you? hwo old are you? HOW ARE YOU? HWO OLD ARE YOU? root:x:0:0:root:/root:/bin/bash
Matching any single character
# egrep 'r..t' a.txt
[] Matches any single character within the specified range, [a b c] a or b or C
# egrep '[eFH]' a.txt
[^] denotes any single character outside the specified range, [^ a b c] is not a and b and C
# egrep '[^eFH]' a.txt
The number of times a single character appears before matching
1)* Matches the previous single character and appears 0, 1, or more times
2)? Match the previous single character, appear 0 or 1 time
3) + Matches the previous single character, appearing at least once, >= once
4) {m} exactly matches the preceding single character m times
5) {m,} matches the previous single character at least m times
6) {n} up to N times
7) {m,n} at least m times, up to N times
Use examples
[root@izpo45bh60h6bsz ~]# vim output_delimiter.txt ab cb a12b aab abb abababababababab
* Match the previous single character, appear 0, 1 or more times
# egrep 'a*b' output_delimiter.txt
Matching. Any time, any single character, any number, any length, any character, (in glob *)
# egrep 'r.*t' a.txt
Match the previous single character and appear 0 or 1 time (in basic regular expressions?)
# egrep 'a?b' output_delimiter.txt # grep 'a\?b' output_delimiter.txt
+ Match the previous single character, appear at least once, >= once (in basic regular expressions+)
# egrep 'a+b' output_delimiter.txt # grep 'a\+b' output_delimiter.txt
{m} exactly matches the preceding single character m times (in basic regular expressions{m})
# egrep 'a{3}b' output_delimiter.txt # grep 'a\{3\}b' output_delimiter.txt
{m,} matches the previous single character at least m times (in basic regular expressions{m,})
# egrep 'a{3,}b' output_delimiter.txt # grep 'a\{3,\}b' output_delimiter.txt
{n} matches the previous single character at most N times (in basic regular expressions{m,})
# egrep 'a{,2}b' output_delimiter.txt # grep 'a\{,2\}b' output_delimiter.txt
{m,n} matches the previous single character at least m times and at most N times (in basic regular expressions{m,n})
# egrep 'a{3,6}b' output_delimiter.txt # grep 'a\{3,6\}b' output_delimiter.txt
Location anchor `The desired matching character must appear at a certain location'
1) ^ Head anchor for the left-most side of the mode. The string matched by a regular expression must appear at the beginning of the line
2) $end-of-line anchor for the rightmost side of the mode. Strings matched by regular expressions must appear at the end of the line
3) ^ pattern $: The whole line can only match this pattern
4) Match blank lines: ^[[: space:]*$blank can be any number of times
5)<or b word head anchor, used on the left side of the word pattern
6)> or b suffix anchoring, for the right side of the word pattern
7) Match the whole word with PATTERN> or bPATTERN\b, used on both left and right sides of the word.
8) Grouping, Backward Reference ()
^ Head anchor for the leftmost side of the mode. The string matched by a regular expression must appear at the beginning of the line
# grep -E '^root' /etc/passwd
Anchor the end of the line for the rightmost side of the mode. Strings matched by regular expressions must appear at the end of the line
# grep -E '/bin/bash$' /etc/passwd
^ pattern $: The whole line can only match this pattern
# grep-E-c'^$'/etc/init.d/functions - c. Display the number of rows in all rows of the matched string
Match blank lines: ^[: space:]*$blank can be any number of times
# grep -E -c '^[[:space:]]*$' /etc/init.d/functions #Match lines that can be blank or have blank characters
\ The < or b prefix anchors, used on the left side of the word pattern
# grep -E '\broot' /etc/passwd
\> Or \b tail anchor, used on the right side of the word pattern
# grep -E 'root\b' /etc/passwd
\ <PATTERN> or \bPATTERN\b Matches the whole word and is used on both left and right sides of the word.
# grep -E '\broot\b' /etc/passwd
Grouping, (Basic Regular Expressions ()) Currently identifies any character with the same component
# cat grep.txt abxy xxxxxxy xyxyxyxyabcxy
# grep -E '(xy)+' grep.txt
Be careful:
When representing the string itself, if the command used is a regular expression character with , no is used; if no , add .
# grep -o '[[:alpha:]_]\+()' /etc/rc.d/init.d/functions #The regular expression character is()
grep -E -o '[[:alpha:]_]+\(\)' /etc/rc.d/init.d/functions #The extended regular expression character is ()
Backward references, the matched content of the pattern in grouping brackets is recorded by the regular expression engine in internal variables named as 1,2,3
# grep -E '(^[[:alnum:]]+\>).*\1$' /etc/passwd
or
[cCat] [c or C or a or t]
C | Cat. C or Cat
(c | C) at Cat or Cat
# vim dsdcI.txt cat Cat c12 C123 at
1. [cCat] Matches any single character within a specified range
# egrep '[cCat]' dsdcI.txt
2. c|Cat Matches c or Cat
# egrep 'c|Cat' dsdcI.txt
3. (c|C)at matching cat or cat
# egrep '(c|C)at' dsdcI.txt