The tools for regular expressions are:grep egrep sed awk
1.grep function
The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users. You can filter the specified rows
2. Format
grep [Options]
3. Main parameters
[Options] Main parameters:
-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.
-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: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[0-9]: Indicates a number
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0, can match more than one character.
. Represents any one character
. * Indicates any one of the characters,s.*d s beginning D end can be matched on
\? indicates that the letter preceding the caret can match s\?d That shows the s in front of the caret or the start of a separate d.
? + for Egrep
-R: Matching directory
--color: Labeling with color
-A: You can display not only matching rows but also the following lines, depending on the number of a followed by a
-B: Not only can you display matching rows, but you can also display the above lines, depending on the value after B
-C: Not only can the matching rows be displayed, but also the top and bottom lines can be displayed, depending on the value after C
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Instance
View Password file cat/etc/passwd
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/8B/4C/wKioL1hJS56Ct-zSAACsq5mKqPA354.png-wh_500x0-wm_3 -wmp_4-s_542479083.png "title=" 1.png "alt=" Wkiol1hjs56ct-zsaacsq5mkqpa354.png-wh_50 "/>
Example 1: Filtering out the line of root under/etc/passwd
grep ' Root '/etc/passwd
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/8B/50/wKiom1hJS6fAbrTQAAATWs0b1IU858.png-wh_500x0-wm_3 -wmp_4-s_731579184.png "title=" 2.png "alt=" Wkiom1hjs6fabrtqaaatws0b1iu858.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Example 2: Displaying and filtering the information for root under/etc/grpup
Cat/etc/group | grep ' Root '/etc/group
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/8B/4C/wKioL1hJS6-DvmeTAAAI4eZMVRI652.png-wh_500x0-wm_3 -wmp_4-s_1333304029.png "title=" 3.png "alt=" Wkiol1hjs6-dvmetaaai4ezmvri652.png-wh_50 "/>
Example 3: Filtering out the root information under/etc/passwd and labeling it with color
grep--color ' root '/etc/passwd
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/8B/50/wKiom1hJS7mRgMDnAAAPh4WsgVc611.png-wh_500x0-wm_3 -wmp_4-s_3611311894.png "title=" 4.png "alt=" Wkiom1hjs7mrgmdnaaaph4wsgvc611.png-wh_50 "/>
Example 4: Filtering out the root information under/etc/passwd and labeling the number of trips with color labels
grep--color-n ' root '/etc/passwd
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/8B/50/wKiom1hJS8CTUdSvAAAQeY6KKhw615.png-wh_500x0-wm_3 -wmp_4-s_921091411.png "title=" 5.png "alt=" Wkiom1hjs8ctudsvaaaqey6kkhw615.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Note: Because grep--color is used more frequently all can do temporary alias settings
Setup method: Alias cg= ' grep--color '
If you want to permanently set the alias method,
Vim. BASHRC
Add a row alias cg= ' grep--color '
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m01/8b/4c/wkiol1hjs9gqk7twaaasjdhqlu4546.png-wh_500x0-wm_3-wmp_4-s_2186388846.png "title = "6.png" alt= "Wkiol1hjs9gqk7twaaasjdhqlu4546.png-wh_50"/>
Example 5: Filtering out the root information under/etc/passwd and displaying only the number of rows
Cg-c ' Root '/etc/passwd
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/8B/4C/wKioL1hJS-2j1AKWAAAG-toCEhY708.png-wh_500x0-wm_3 -wmp_4-s_3669768374.png "title=" 7.png "alt=" Wkiol1hjs-2j1akwaaag-tocehy708.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Example 6: Filtering out rows that do not contain root information under/etc/passwd
Cg-v ' Root '/etc/passwd
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/8b/50/wkiom1hjs_mtvomaaabofj7d2ky890.png-wh_500x0-wm_3-wmp_4-s_3657408918.png "title = "8.png" alt= "Wkiom1hjs_mtvomaaabofj7d2ky890.png-wh_50"/>
Example 7: Filter out the information about root under/etc/passwd and display more information on the following three lines
Cg-n-A 3 ' root '/etc/passwd
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8B/4C/wKioL1hJTALD7wSHAAAxEcyvFhQ059.png-wh_500x0-wm_3 -wmp_4-s_857768425.png "title=" 9.png "alt=" Wkiol1hjtald7wshaaaxecyvfhq059.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Example 8: Filter out information about root under/etc/passwd and display more than four lines above
Cg-n-B 4 ' root '/etc/passwd
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/8B/4D/wKioL1hJTBHB0nZ-AAAlGEznPVs501.png-wh_500x0-wm_3 -wmp_4-s_1917255271.png "title=" 10.png "alt=" Wkiol1hjtbhb0nz-aaalgeznpvs501.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Example 9: Filtering out information about games under/etc/passwd and displaying more than two lines of information
Cg-n-C 2 ' games '/etc/passwd
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/8b/50/wkiom1hjtcfc7jq6aaajn_ukjdy116.png-wh_500x0-wm_3-wmp_4-s_3781466992.png "title = "11.png" alt= "Wkiom1hjtcfc7jq6aaajn_ukjdy116.png-wh_50"/>
Detailed instructions and examples of grep filtering