Detailed description of grep, egrep, and fgrep usage of text search commands
I. Learning Objectives
Understand and be familiar with grep, egrep, and fgrep commands.
Ii. Learning Content
1. Meaning and usage format of grep, egrep, and fgrep commands:
Grep: a command that uses the mode defined by the basic regular expression to filter text.
# Grep [Options] pattern [file,...]
Egrep: a command that uses the extended regular expression mode to filter text.
# Egrep [Options] pattern [file,...]
Fgrep: does not support regular expressions. It is a command that uses the pattern of the meaning of the text to filter the text.
# Fgrep [Options] pattern [file,...]
Note: pattren: A match condition formed by a combination of text characters and regular expression metacharacters
2. options commonly used in grep commands
-I: Ignore the case sensitivity of characters.
-- Color: add color to the content of Pattern
-V: displays unmatched rows.
-O: only the strings matching the pattern are displayed.
-E: use the extended regular expression = egrep
-F: = fgrep
-A n: displays the last n rows of the content of the pattern at the same time.
-B N: displays the first n rows of the content of the pattern.
-C n: displays n rows before and after the content of the pattern.
3. Understand what a regular expression is.
Regular Expressions include basic regular expressions and extended regular expressions.
(1) Basic Regular Expressions:
Character match:
.: Match any single character
[]: Match any single character in the specified range
[^]: Any single character out of the specified range
[: Space:]: white space
[: Punct:]: punctuation
[: Lower:]: lowercase letter
[: Upper:]: uppercase letters
[: Alpha:]: uppercase/lowercase letters
[: Digit:]: Number
[: Alnum:]: Numbers and uppercase/lowercase letters
Matching times:
*: Match any character before it.
. *: Match any length and number of times of the preceding characters
\? : Match the previous character once or 0 times
\ {M, N \}: match the first character at least m times, at most N times
Positioning:
^: Specifies the beginning of the line. Any content after this character must appear at the beginning of the line.
$: Specifies the end of the line. Any content before this character must appear at the end of the line.
^ $: Blank line,
\ <: The content after it must appear as the word Header
\>: The content before it must appear at the end of the word.
GROUP:
\ (\): The content of the brackets as a whole
\ 1: All content of the first left brace and the corresponding right Brace
\ 2: All content of the second left brace and corresponding right Brace
.....
(2) extended regular expressions:
Character matching: (same as in the basic regular expression)
Matching times:
*: Match any character before it.
? : Match the previous character once or 0 times
+: Match the first character at least once
{M, n}: match the first character at least m times, at most N times
Anchoring match: (same as in the basic regular expression)
GROUP: (): Take the content in the brackets as a whole \ 1 \ 2 \ 3 ....
Or: |: indicates or
4. command usage demonstration
----------------------------------------------------------------------
1. display the lines starting with "S" of the/proc/meminfo file with no size difference;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/00/wKioL1PPxgziGJPRAACvG_o3Dg8364.jpg "Title =" 36020140723211148642.jpg" alt = "wkiol1ppxgzigjpraacvg_o3dg8364.jpg"/>
2. display the rows ending with nologin in/etc/passwd;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKiom1PPxQfzp9jSAAPgWimOpYQ344.jpg "Title =" 36020140723211404559.jpg" alt = "wkiom1ppxrjzp9jsaapgwimopyx64 4.jpg"/>
Retrieve the list of users whose default shell is/sbin/nologin.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/00/wKioL1PPxjaAb6fqAADviBzE9qU389.jpg "Title =" 36020140723211812975.jpg" alt = "wkiol1ppxjaab6fqaadvibze9qu389.jpg"/>
Obtain the username of the user whose default shell is Bash and whose user ID is the smallest
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/00/wKiom1PPxTDi-iGWAABth_8MnDk892.jpg "Title =" 36020140723212214331.jpg" alt = "wKiom1PPxTDi-iGWAABth_8MnDk892.jpg"/>
3. the/etc/inputrc starts with #, followed by one or more blank characters, and followed by any non-blank characters;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKioL1PPxmCzFWErAAEu9UgOCQU114.jpg "Title =" 36020140723213752297.jpg" alt = "wkiol1ppxmczfweraaeu9ugocqu114.jpg"/>
4. Display/etc/passwd contains: a number (that is, a number between two colons) rows;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKiom1PPxebxmIl0AAFMKGBOqWk500.jpg "Title =" 36020140723214134682.jpg" alt = "wkiom1ppxebxmil0aafmkgboqwk500.jpg"/>
5. display the lines starting with one or more blank characters in the/boot/GRUB/grub. conf file;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKioL1PPxuLSLijxAAD33ttdrEM610.jpg "Title =" 36020140723214515201.jpg" alt = "wkiol1ppxulslijxaad33ttdrem610.jpg"/>
7. Find the one-digit and two-digit numbers in the/proc/cpuinfo file.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKiom1PPxfvB55gYAADVOLec7Co898.jpg "Title =" 36020140723215116706.jpg" alt = "wkiom1ppxfvb55gyaadvolec7co898.jpg"/>
8. Locate the integer between 1-In the ifconfig command result;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/00/wKioL1PPxyLzXv_JAAHEQaNgjwQ131.jpg "Title =" 36020140723220306301.jpg" alt = "wkiol1ppxylzxv_jaaheqang1_q131.jpg"/>
9. query the account information of the user whose name is student (the row must appear first) on the current system. The file is/etc/passwd.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKioL1PPx0jhEtcfAABe2HDgo20730.jpg "Title =" 36020140723220848134.jpg" alt = "wkiol1ppx0jhetcfaabe2hdgo20730.jpg"/>
11. All files whose names start with lowercase letters and end with numbers and do not contain spaces are displayed;
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/00/wKiom1PPxjnxU8RkAADul40C_HU940.jpg "Title =" 36020140723222043222.jpg" alt = "wkiom1ppxjnxu8rkaadul40c_hu940.jpg"/>
If you have read this article and find it a problem, you are welcome to raise it. You are also welcome to discuss it and raise it together!
This article from the "Lost Sao Nian" blog, please be sure to keep this source http://8855546.blog.51cto.com/8845546/1528490