The role of grep: search for text according to the specified pattern, displaying the lines of text that match to
Syntax format: grep "PATTERN" file
Pattern is a regular expression, and grep is generally used in conjunction with regular expressions, so we'll start with a regular expression under the shell. Regular expressions, in simple terms, are matching rules. A line of text matches a regular expression with only two results, either matched or mismatched.
Next we introduce the regular expression in detail
1. Meta-characters:
.: Matches any single character
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
Character Set:
[:d Igit:] or [0-9] number
[: Lower:] or [A-z] lowercase letter
[: Upper:] or [A-z] capital Letter
[:p UNCT:] Punctuation
[: Space:] Blank
[: Alpha:] [A-za-z] Letter
[: Alnum:] [a-za-z0-9] Letter + number
[^[:d Igit:]] Non-digital
2. Number of matches (greedy mode, always match to last):
*: Matches any of its preceding characters any time
A, B, AB, AAB, ACB, ADB, AMNB
A*b, A?b
A.*b
. *: Any character of any length (note that it differs from a wildcard)
\?: match its preceding characters 1 or 0 times don't forget to escape \
\{m,n\}: Match its preceding characters at least m times, up to n times do not forget to escape \
\{1,\} at least 1 times
\{0,3\} 0 to 3 times
3. Location anchoring:
^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line
$: Anchor line end, any content in front of this character must appear at the end of the row
^$: Blank Line
\< or \b: Anchor word, any character following it must appear as the first word
\> or \b: anchors the ending, any character preceding it must appear as the tail of the word
4. Group: Don't forget to escape
\(\)
\ (ab\) * AB is seen as a whole
Back to reference
\1: Refers to the first opening parenthesis and all the contents of the corresponding closing parenthesis
[2]
[3]
Light says do not practice false bashi. Here are a few examples
1. Display the lines in the/proc/meminfo file that begin with a non-differentiated s;
Grep-i ' ^s '/proc/meminfo
grep ' ^[ss] '/proc/meminfo
2. Display the line ending with Nologin in/etc/passwd;
grep ' nologin$ '/etc/passwd
Remove the default shell for/sbin/nologin user list
grep "nologin$ '/etc/passwd | Cut-d:-f1
Remove the user name of the user whose default shell is bash with the lowest user ID number
grep ' bash$ '/etc/passwd | Sort-n-T:-k3 | head-1 | Cut-d:-f1
3, display/etc/inittab in the beginning of #, and followed by one or more white space characters, followed by any non-whitespace character line;
grep "^#[[:space:]]\{1,\}[^[:space:]]"/etc/inittab
4, display/etc/inittab contains: A number: (that is, two colons in the middle of a number) line;
grep ': [0-9]: '/etc/inittab
5. Display lines that start with one or more whitespace characters in the/boot/grub/grub.conf file;
grep ' ^[[:space:]]\{1,\} '/boot/grub/grub.conf
6. Display a line in the/etc/inittab file that begins with a number and ends with a number that is the same as the beginning number;
grep ' ^\ ([0-9]\). *\1$ '/etc/inittab
7. Analyze the characteristics of the first two lines in the following text in the/etc/inittab file (the number must be the same in each line), and write a pattern that can be found exactly like two lines:
L1:1:WAIT:/ETC/RC.D/RC 1
L3:3:WAIT:/ETC/RC.D/RC 3
grep ' ^l\ ([0-9]\): \1.*\1$ '/etc/inittab
This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1826179
Linux Basics (9) Text processing grep of the Three Musketeers