Learn about grep and regular expressions

Source: Internet
Author: User
Regular Expressions: a regular expression that consists of a bunch of metacharacters. these metacharacters are combined according to certain rules to filter and match different contents. (In short, a regular expression is a filter composed of metacharacters.) regular expression classification 1. basic regular expression... Info & nbsp

 

Regular expression to quickly adjust grep

Regular expression: it is composed of a bunch of metacharacters. these metacharacters are combined according to certain rules to filter and match different contents. (In short, a regular expression is a filter composed of metacharacters)

Regular expression classification

1. basic regular expression: a standard regular expression that supports basic metacharacters.

The metacharacters supported by the basic regular expression have the following meanings:

 

 

2. Extended Regular Expressions: more metacharacters are supported than the basic regular expressions, but the extended regular expressions do not support metacharacters supported by some basic regular expressions.

Extended Regular expressions support metacharacters:

 

Through comparison, we will find that the metacharacters supported in basic regular expressions are not all supported in extended regular expressions.

 

Grep

Grep fully executes the regular expression and displays the matching content. In other words, grep is used to execute a filter composed of metacharacters and display information that matches the filter.

Grep executes the regular expression in the format of grep "keyword" file

Grep is a command used to execute basic regular expressions. it cannot recognize metacharacters in extended regular expressions.

Grep-E = egrep is used to execute extended regular expressions. because the extended regular expressions do not contain all basic regular expressions, egrep cannot execute basic regular expressions.

Next, let's take a look at the effect of grep and regular expressions through several exercises.

Answer

 

1. grep -- color "^ [sS]"/pro/meminfo (-- color indicates that the matching content is displayed in red to check the execution result, for convenience, you can also use the alias grep = 'grep -- color' command to set the alias of the command so that the grep and grep -- color Commands have the same effect)

2. grep "nologin $"/etc/passwd

3. grep "^ # [[: space:] \ (1, \). *"/etc/inittab

4. grep ": [0-9]:"/etc/inittab

5. grep "[[: space:] \ (1, \). *"/boot/grub. conf

6. grep "^ \ ([0-9] \). * \ 1 $"/etc/

7. ifconfig | grep -- color "inet addr" | grep-v '2017. 0.0.1 '| cut-d:-f2 | cut-d ""-f1

8. grep -- color "[0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. "/etc/sysconfig/network-spcripts/ifcfg-eth0 the answer to using an extended regular expression is as follows:

Grep-E "([0-9] \ {1, 3 \}\.) {3} [0-9] \ {1, 3} "/etc/sysconfig/network-spcripts/ifcfg-eth0

This article is from the "redhat" blog

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.