Need for text lookup
Grep,egrep, Fgrep
grep: Search for text based on pattern and display the text lines of the review mode
Pattern: Text Word wildcards regular the expression's meta-character combination to match the condition
grep [Options] PATTERN [FILE ...]
-i:ignore Case ignores capitalization
--color: Matched to the word Fugauliang display
-V: Show rows that are not matched by the pattern
-O: Displays only strings that are matched by the pattern
*: Any character of any length
? : Any single character
[] :
[^]
Regular expression: REGular expression,regexp
Metacharacters
. : Match any single character grep ' R. T '/etc/passwd
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
Character set: [:d igit:], [: Lower:], [: Upper:], [:p UNCT:], [: Space:], [: Alpha:], [: Alnum:]
Number of matches (greedy mode):
*: Matches any of its preceding characters any time
A, B, AB, AAB, ACB, ADB, AMNB
A*b:b before matching any A, can not, the above match is B,ab,aab, ADB,AMNB
A.*b:ab,aab,acb,adb,amnb
. *: Any character of any length
\? : matches the preceding character 1 or 0 times
A\?b:b,ab,aab,acb,amnb
\{m,n\}: Matches the preceding character at least m times, up to N times
Location anchoring:
^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line
grep ' ^r. T '/etc/passwd
$: Anchor line end, any content in front of this character must appear at the end of the row
grep ' om$ '/etc/passwd
Numbers ending with:
grep ' [[:d igit:]]$ '/etc/inittab
^$: Blank Line
\< or \b: Anchor word, any character following it must appear as the first word
\> or \b: anchors the ending, any character in front of which must appear as the tail of the word
\<root\>: Root must appear for a whole word
Group:
\(\)
\ (ab\) *: AB appears together 0 or more times
Back to reference
\1: Apply the first opening parenthesis and all the content that corresponds to the closing parenthesis
[2]
[3]
L.. E
Practice:
1. Display the lines in the/proc/meminfo file that begin with a non-differentiated s;
# grep-i ' ^s '/proc/meminfo
# grep ' ^[ss] '/proc/meninfo
2. Display the line ending with Nologin in/etc/passwd;
# grep ' nologin$ '/etc/passwd
Remove the default shell for/sbin/nologin user list
# grep '/sbin/nologin$ ' | 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
Practice:
1, find out a file, 1 digits, or 2-digit number;
# grep ' [0-9]\{1,2\} '/proc/cpuinfo
2. Find the integer between 1-255 in the result of ifconfig command;
#
3. Find information about the account of the user whose name is student (must appear at the beginning of the line) on the current system, the file is/etc/passwd
# grep ' ^student\> '
grep ' ^student\> '/etc/passwd | Cut-d:-F3
Id-u Student
Student1
Student2
Exercise: 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 row), 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
Linux--grep and regular expressions