grep command
1, grep program: Linux Three Musketeers–grep, awk, sed
2, GrepL: text line filtering tool
< p> sed: text line editor
Awk: report generator (formatting text output)
3, grep contains three commands: grep, egrep, fgrep, they are Used for pattern matching
Egrep=grep -E //Use extended regular expression to match
Matching
*grep uses regular expressions for text matching by default
4, grep command usage: grep [option]…..PATTERN [filename]
< p>5. Common parameters of Grep: -E (supports the use of extended regular expressions, regexp (regular expression))
-P (use the regular expression engine of the per language to search (each The regular expression engines of different languages are different, even the regular expression engine used by sed grep awk is different)))
-i (ignore case), -v (invert the selection), -n (Display line number), –color (display coloring)
-o (only output the matched content, the default output is the matched line)
PATTERN—Regular Expression
1. Function: Use some special characters to express the content of a type of character, and then give it to the previous command for execution. If you use the meaning of the special character itself, you need to escape it with \; < /p>
2. Character matching:. (represents any character, equivalent to?), [] (a character in the matching range), [^] (a character outside the range)
Character class: [:digit:] (number) [:alnum:] (number and uppercase and lowercase letters) [:alpha:] (upper and lowercase letters) [:lower:] (lowercase letters)
[:upper:] (uppercase letter) [:space:] (space) [:punct:] (special symbol)
3. Number of matching: * (matching the preceding character 0 times to n (unnumbered) ) Times)
? (Match the preceding character 0 to 1 time)
+ (match the preceding character 1 to n times)
\{m\} (match the preceding character m Times)
\{m,n\} (match the preceding character m to n (fixed value) times)
\{m,\} (match the preceding character at least m times)
4. Position anchoring: ^ (anchoring the beginning of the line)
$ (anchoring the end of the line)
\b (anchoring the word Head and anchor ending)
\>(anchored ending)
\<(anchored ending)
5. Grouping characteristics: By default , The Linux system will group and specify the variable, the representation of the variable is \1\2\3..
Grouping character: grouping character: \(\) (example: \(abc\)*—- -Match 0 to n times abc)
if statement:
Under certain conditions, if the conditions are not met, we must manually exit the program, otherwise the following code cannot be executed;
Specify the output code: exit 0, exit 1
When the program error is output, it can be used to judge the program error.
Exit exit code, the following value is You can define it yourself. Generally, the correct value is 0, and the error value is non-zero.
Exercise:
1. Display the line starting with size s in the /proc/meminfo file
1
|
grep< /code> |
2, display / etc/passwd file without /bin /bash ending line
1 |
grep - v "/bin/bash$" /etc/passwd |
3, display/ The username of the user with the largest UID in the etc/passwd file
1
|
sort -n -t:- k3 /etc/passwd | tail -1 | cut -d: -f1 |
4. If the user root exists, display its default shell program
Method 1:
1
|
grep "^root\>" /etc/passwd &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7 |