a basic introduction to grep and Egrep :
First, we need to know that grep 's command itself means:
grep [Global search REgular expression and print out of the line] globally searches the contents of the regular expression and prints out the matches in the row
grep is a very common but powerful search tool that is based on regular expressions, searches for strings that meet the requirements in a text file, and displays the line where the matched string is located.
The grep search is displayed on a single line, and of course we can use some special options to show only what we've matched, and this will be explained to you later, and grep will typically be associated with (|) Pipeline
grep+ Basic Regular Expressions + pipelines are the most common combinations we use in our actual operations, as in combination.
Second, the use of grep command method
Use the syntax format:
grep [OPTIONS] PATTERN [FILE ...]
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-E: Egrep, with extended regular expressions using the same usage as grep
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Invert selection to display all lines that do not contain matching text
-O: Displays only the matched string, not the line where the string is located
-A +#: Show line after matching line
-B +#: Show line before matching line
-C +#: Show lines before and after matching lines
Egrep = GREP-E usage is the same as grep, used with extended regular expressions to print out the matching string. We've been talking about grep or egrep in conjunction with regular expressions, so what is a regular expression? Here is an introduction to Linux, a very important knowledge of the basics, regular expressions
Third, the regular expression Re[regular expression]
Regular represents the basic concept
is a pattern written by a class of characters that uses a single string to describe and match a series of strings that conform to a certain syntactic rule. Many of these characters do not represent their literal meaning, but rather the expression of control or wildcard functions, concise, the regular expression is the use of some combination of characters to the rules, to cooperate with the search command to allow users to complete the purpose of quick and easy.
Regular expressions are divided into two types of regular expression and extended regular expression.
A) Basic Regular expression:
Basic regular expressions typically use the following meta-characters:
1. Character Matching
. : Represents any single character
[]: matches any single character within the specified range
[0-9] Match any single number within 0-9 with [[:d Igit:]]
[A-z] Match any single letter in a-Z with [[: Lower:]]
[A-z] similar to [a-z] [[: Upper:]]
[[: Space:]] Match space
[[:d Igit:]] match any single number
[[: Lower:]] match any single lowercase letter
[[: Upper:]] match any single uppercase letter
[[:p UNCT:]] match any single character
[[: Alpha:]] match any single letter
[[: Alnum:]] Match any single letter or number
2, number of times match metacharacters, to specify the number of occurrences of the preceding character
*: Indicates any length, the characters in front of him can appear any time
\? : represents 0 or 1 times, the characters in front of it are optional
For example: X\?y has the following possibilities:
y or xy
\{m\}: M times, it before the character to appear m times
Example: X\{2\}y
Possible: XY, Xxy, y, Xxxxy, XYY
\{m,n\}: At least m times, up to N times
Example: X\{2,5\}y
may be: xy, y, XXY
\{m,\}: At least m times
\{0,n\}: Up to n times
. *: Any character of any length
For example, a line that starts with at least one whitespace character in/boot/grub/grub.conf;
[[email protected] ~]# grep "^[[:space:]]\{1,\}"/boot/grub/grub.conf
Root (hd0,0)
kernel/vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg0-root rd_no_luks rd_no_dm LANG=en_US. UTF-8 rd_lvm_lv=vg0/swap rd_no_md sysfont=latarcyrheb-sun16 crashkernel=auto rd_lvm_lv=vg0/root KEYBOARDTYPE=pc Keytable=us rhgb Crashkernel=auto Quiet rhgb quiet
Initrd/initramfs-2.6.32-431.el6.x86_64.img
3. Positional anchor metacharacters To specify the exact location of the line where the match character is located
^: anchor at the beginning of the line;
Followed characters appear at the beginning of the line
For example: Search for lines in the/etc/passwd file that begin with a
[[email protected] ~]# grep--color=auto "^a"/etc/passwd
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Avahi-autoipd:x:170:170:avahi Ipv4ll Stack:/var/lib/avahi-autoipd:/sbin/nologin
Abrt:x:173:173::/etc/abrt:/sbin/nologin
Apache:x:48:48:apache:/var/www:/sbin/nologin
$: End of line anchoring:
followed by a character that appears at the end of the line
For example: Search for lines in the/etc/passwd file that end with bash (because there's too much content, I'll just list a few lines to do the example)
[[Email protected] ~] #grep "\bbash\b$"/etc/passwd
X1:x:3320:3320::/home/x1:/bin/bash
X2:x:3321:3321::/home/x2:/bin/bash
Mmm1:x:3322:3322::/home/mmm1:/bin/bash
Mmm2:x:3323:3323::/home/mmm2:/bin/bash
A string that consists of consecutive characters that do not contain special characters is called a word:
\< or \b: The first word, appearing on the left side of the word,
> or \b: Ending, appearing on the right side of the word
^$: Indicates a blank line
4. Grouping and referencing
Group:
\ (\): Use the content in parentheses as a single character
Reference:
\#: Refers to the content that the nth parenthesis matches to, not the schema itself.
For example: \ (xyz\). *\1 = xyz.*xyz
II) extended Regular expression
Extended regular expressions mainly refer to Egrep Fgrep and several related special meta-characters
Special Meta characters
+ indicates that the character before it appears at least once.
? Represents 0 or 11 characters, unlike the \? In the basic regular expression, please note that
| Use or to select characters on both sides.
() user group string, that is, the meta-word characters as a group of a whole.
Iv. examples of combined use of 2 grep + regular Expressions:
(1) Display the information of root, fedora or User1 user on the current system;
[[email protected] ~]#egrep "^ (root|fedora|user1):"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
In this example, the "|" is used to anchor ^ and extend the regular expression at the beginning of the line, so use egrep, because every fedora and User1 user is added to my system, only the root information is displayed.
(2) Use the echo command to output a path and then use grep to remove its base name
[email protected] ~]#echo "/etc/passwd/" | Grep-o-E "[[: alnum:]]+/?$"
passwd/
In this example, grep, pipeline, and extended regular expressions can be used as the most commonly used patterns to deal with the problem of teaching complex points, and also use the various meta-characters of regular expressions.
This article is from the "Linux Small Pot Friends" blog, please be sure to keep this source http://ny0716.blog.51cto.com/9154254/1437660