Grep and regular expressions
Need for text lookup:
Grep,egrep,fgrep
Grep: search for text based on the pattern and display lines of text that match the pattern.
Pattern: a matching condition formed by the combination of text characters and metacharacters of regular expressions
Grep [OPTIONS] PATTERN [FILE...]
-I: ignore case
-- color: match highlight color
-v: displays lines that have not been matched
-o: only the strings matched by the pattern are displayed
Regular expressions: GEGular,EXPression,REGEXP
Metacharacters:
.: match any single character
Number of matches:
*: match the characters before them any number of times
[]: matches any single character in the specified range
[^]: matches any single character outside the specified range
[: digit:], [: lower:], [: upper:], [: punct:], [: space:], [: alpha:], [: alnum:] this is a collection of characters
Add double parentheses when using, for example: [: digit:]
A,b,ab,aab,acb,adb,amnb
Axib
[root@one ~] # grep ahumb test.txt
B
Ab
Aab
Acb
Adb
Amnb
A. Roomb
[root@one ~] # grep 'a.roomb' test.txt
Ab
Aab
Acb
Adb
Amnb
\?: match the character in front of it 1 or 0 times
[root@one] # grep'a\? B 'test.txt
B
Ab
Aab
Acb
Adb
Amnb
\ {mdirection n\}: match the characters before them at least m times and n times at most
\ {1,\} at least once, no limit
\ {0pr 3\} 3 times at most
[root@one ~] # grep'a\ {1Jing 3\} b'test.txt
Ab
Aab
Position Anchor:
^: anchor the beginning of the line, and anything after this character must appear at the beginning of the line
[root@one ~] # grep'^ r.. t'/ etc/passwd
Root:x:0:0:root:/root:/bin/bash
$: anchors the end of the line, and anything before the secondary character must appear at the end of the line
[root@one ~] # grep'b.. hackers'/ etc/passwd
Root:x:0:0:root:/root:/bin/bash
Vagrant:x:500:500:vagrant:/home/vagrant:/bin/bash
Tom:x:501:501::/home/tom:/bin/bash
User3:x:2002:1002:User3,be,110,119:/home/user3:/bin/bash
User4:x:1003:1003:Tony Blare:/home/blare:/bin/bash
User7:x:1006:1006::/home/user7:/bin/bash
Apache:x:497:497::/home/apache:/bin/bash
Hadoop:x:2003:2003::/home/hadoop:/bin/bash
^ $: blank line
Any character in front of it must appear at the end of the word
\ b put before or after a word to indicate an anchor prefix or suffix
Grouping:
\ (\)
\ (ab\) *
Latter item reference
\ 1: reference everything contained in the first left parenthesis and the corresponding closing parenthesis.
\ 2:
\ 3:
He love his lover.
She like her liker.
He like his lover.
[root@one ~] # grep'\ (ab\) * 'test.txt
A
B
Ab
Aab
Acb
Adb
Amnb