The use of grep, egrep and regular expressions for the Three Musketeers of Linux

Source: Internet
Author: User
Tags readable line editor egrep


Linux Three Musketeers is a very powerful text processing tool in Linux, Master Three Musketeers, the text processing already presumably will have three swords in hand, the world I have feelings, three musketeers of the grep family is good at text search, support for regular expression text search, make grep very strong, the following content on grep, Egrep and Regular Expressions expand


Linux Text Tool Three musketeers:
grep, Egrep, Fgrep: Text Search Tool

Sed: Stream editor, also line editor

awk: Text formatting tool, version Report Builder


Regular expression: a pattern (pattern) written by a class of characters

role: Text filtering with a text search tool that supports the use of regular expressions

Metacharacters: Similar to wildcard characters, does not represent the meaning of the character itself, for the description of additional functionality

Classification: basic regular expressions and extended regular expressions


Basic Regular Expression (BRE)

Character matching:
 . : Any single character

[]: Any single character within the specified range

[0-9],[[:d Igit:]]: All numbers

[A-z],[[:lower:]]: lowercase letters

[A-z],[[:upper:]]: Uppercase

[[: Alpha:]]: All uppercase and lowercase letters

[[: Alnum:]]: Numbers and all uppercase and lowercase letters

[[: Space:]]: white space characters

[[:p UNCT:]]: special characters

[^]: Any single character outside the specified range

number of matches: matches the number of occurrences of the previous character of a match
*: The previous character appears any number of times, such as: A *, refers to a can occur any number of times, including 0 times

\?: previous character 0 or 1 times

\+: The previous character appears at least 1 times

\{m\}: The previous character appears m times for exact match

\{m,n\}: The previous character appears at least m times, up to N times

\{m,\}: The previous character appears at least m times

\{0,n}: The previous character appears at most n times

position anchor: limits where the character that matches to appears
^: Anchor at the beginning of the line

$: End of line anchoring

^$: Blank Line

^[[:space:]]*$: Blank line with white space characters

\< or \b: The first anchor of the word

\> or \b: Final anchoring

Group:
\ (\): Groups characters, such as: \ (ab\)

References:
\1: A reference to the first ' opening parenthesis in the expression and corresponding to the right parenthesis ' in the match to the content once, that is, the contents of the parentheses are displayed at the reference again


Grep:global search REgular expression and Print out of the line
Supports text search using regular expressions and prints the rows to which the pattern matches

grep [OPTIONS] PATTERN [FILE ...]
--color=auto: Color highlighting of matched characters (default grep= ' grep--color=auto in Centos7)

-I: Ignore case

-O: Show only what matches to

-N: Show line numbers

-v,--invert-match: Reverse, display the opposite of the matching pattern

-e: Extended Regular expression support

-Q,--quiet,--silient: Silent mode, do not output any information


Instance:
(1) Remove the user name in the/etc/passwd with "s" User information
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/08/wKiom1betjmi6Cd4AAATxJW1nLg080.png "title=" 2.png " alt= "Wkiom1betjmi6cd4aaatxjw1nlg080.png"/>

(2) Find the three-bit or four-digit number in the/etc/passwd file
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/07/wKioL1betuuRmIE2AAAX0nKO-48660.png "title=" 3.png " alt= "Wkiol1betuurmie2aaax0nko-48660.png"/>

(3) Remove user information from shutdown users
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/08/wKiom1bet_TRWx5CAAALMVzXhYQ126.png "title=" 9.png " alt= "Wkiom1bet_trwx5caaalmvzxhyq126.png"/>

(4) Remove user information other than the root user
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/07/wKioL1beuMXhyv2sAABB0UzciRs047.png "title=" 4.png " alt= "Wkiol1beumxhyv2saabb0uzcirs047.png"/>

(5) Remove blank lines from the/ETC/BASHRC file and print the number of trips
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/07/wKioL1beuSzAimf0AAAGRLAarGo808.png "title=" 5.png " alt= "Wkiol1beuszaimf0aaagrlaargo808.png"/>

(6) Find the/etc/grub2.cfg file, beginning with at least one whitespace character, followed by a line with non-whitespace characters
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/08/wKiom1beuOfhiSwnAAAj3Af1e9k192.png "title=" 6.png " alt= "Wkiom1beuofhiswnaaaj3af1e9k192.png"/>

(7) Remove the default shell as Nologin, the user ID of the largest user
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/07/wKioL1beue3hhyc_AAALkvxRCC4851.png "title=" 7.png " alt= "Wkiol1beue3hhyc_aaalkvxrcc4851.png"/>

(8) Remove the user name and the default shell for the same user information
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/08/wKiom1beub7Q9mFBAAARW-gjgOw583.png "title=" 8.png " alt= "Wkiom1beub7q9mfbaaarw-gjgow583.png"/>


Extended Regular expression: (ERE)

character matching: Same as basic regular expression

.: Any single character

[]: Any single character within the specified range

[0-9],[[:d Igit:]]: All numbers

[A-z],[[:lower:]]: lowercase letters

[A-z],[[:upper:]]: Uppercase

[[: Alpha:]]: All uppercase and lowercase letters

[[: Alnum:]]: Numbers and all uppercase and lowercase letters

[[: Space:]]: white space characters

[[:p UNCT:]]: special characters

[^]: Any single character outside the specified range

number of matches: The escape character is less than the basic regular expression "\"
*: Match any number of times

?: 0 or 1 times

+: At least once

{m}: matches M-Times

{0,m}: matches up to M times

{m,}: matches at least m times

{n,m}: up to M times, at least n times

positional anchoring: Also with basic regular expressions
^: Anchor at the beginning of the line

$: End of line anchoring

^$: Blank Line

\< or \b: The first anchor of the word

\> or \b: Final anchoring

Group:
(AB)

References:
\1

or:
|:a|b,a or B
(c|c) At,cat or cat


Basic regular and extended regular table metacharacters use Variance Summary:
The difference between the basic regular expression and the meta-character of an extended regular expression is that the extended regular expression matches and groups without the escape character "\" and more "or" matches


Extended version of Egrep:grep
Supports text search with extended regular expressions and prints the rows to which the pattern matches

egrep [OPTIONS] PATTERN [FILE ...]

-F,--fixed-strings: Supports the use of fixed strings, does not support regular expressions, and is equivalent to Fgrep

-G,--basic-regexp: Supports the use of basic regular expressions

-P,--perl-regexp: supports the use of pcre regular expressions
-E pattern,--regexp=pattern: Multi-mode mechanism

-F file,--file=file:file a text file containing a pattern for each line, the grep script
-A NUM,--after-context=num, displays the n rows following the matching line

-B NUM,--before-context=num, shows the line that matches the front n rows

-C NUM,-num,--context=num, showing n rows before and after matching rows

Such as:

-A 2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/07/wKioL1bewyviqhQmAAAMiNU2RpA650.png "title=" after. png " alt= "Wkiol1bewyviqhqmaaaminu2rpa650.png"/>

-B 2

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/07/wKioL1bew6XD5cK7AAARl0Ury1g791.png "title=" before. png " alt= "Wkiol1bew6xd5ck7aaarl0ury1g791.png"/>

-C 2

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/07/wKioL1bew3XSPTW8AAAXxLjfBuA076.png "title=" before and after. png "alt=" Wkiol1bew3xsptw8aaaxxljfbua076.png "/>

The GREP,EGREP option is basically generic, and the usual options listed in grep are not listed here.


Example: (The exercises in grep are also implemented with EGREP, and the following exercises mainly demonstrate the "|" or for use)

(1) Remove a file or directory with readable permissions from the/tmp directory

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/08/wKioL1be0fDgV2unAAAYiIzjAy8837.png "title=" belong to group readable. png "alt=" Wkiol1be0fdgv2unaaayiizjay8837.png "/>

(2) Remove the integer from # 1-255 in the # IP add show result

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/08/wKioL1be1WOzPjZOAAAw0dKsK08754.png "title=" 1-255. PNG "alt=" Wkiol1be1wozpjzoaaaw0dksk08754.png "/>


Although the tool is strong, only proficiency can be turned into self-use, familiar with the command to use only one way-the keyboard knocked, do not ask me why, I can only use someone told me to tell you: "The keyboard knocked rotten, monthly salary 50,000"


This article is from "Rock blog" blog, please be sure to keep this source http://johnsonxu.blog.51cto.com/11214707/1748937

The use of grep, egrep and regular expressions for the Three Musketeers of Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.