RHCE path-Regular Expression

Source: Internet
Author: User
Tags processing text
1. What is a regular expression? To manage hosts, any experienced System Administrator will tell you that "Regular Expressions are important ". Why is it important? In the process of daily host management, the main thing is to process text characters, while regular expressions are just a tool that is indispensable for processing text. In short, a regular expression is a string processing method. It processes strings in the unit of action. With the help of some special symbols, you can easily process text. For general users, there are not many opportunities to use regular expressions.

1. What is a regular expression?

To manage hosts, any experienced System Administrator will tell you that "Regular Expressions are important ". Why is it important? Because in the Process of daily host management,

The main thing is to process text characters, while regular expressions are exactly the tools that are essential for processing text.

In short, a regular expression is a string processing method. It processes strings in the unit of action. With the help of some special symbols, you can easily process text.

Generally, there are not many opportunities to use regular expressions. However, to become a system administrator, regular expressions cannot be learned. The reason is that the system generates more information than you can imagine every day.

Because the amount of data in the system is too large, it is unrealistic to ask the system administrator to view so much information every day. It is wise to find out the problematic lines from thousands of lines for processing. At this time, you have to use a regular expression. In this way, the Administrator's work will be very easy, of course, the use of regular expressions is not so good, you will fall in love with it after a deep understanding.

There are two types of Regular Expressions: Basic Regular Expressions and extended regular expressions.

The following describes the two regular expressions.

I. Basic regular expressions.

Regular Expressions are the standard way to process strings. They need to be assisted by Supported tools. Therefore, here we first introduce a simple command grep. After the introduction, let's talk about the string processing capability of the expression.

First, let's take a look at the grep syntax.

Grep [-acinv] 'search string 'filename'

Parameter description:

-A: searches for data in a binary file in text format.

-C: calculates the number of times a 'search string' is found.

-I: The Case sensitivity is ignored, so the case sensitivity is the same.

-N: Output row number.

-V: reverse selection, that is, the rows without 'search string' are displayed.

For example

# Grep-v 'root'/var/log/secure

Display the rows without root in/var/log/secure.

# Grep 'root'/var/log/secure

Display the rows with root under/var/log/secure

Next we will talk about regular expressions in combination with grep.

Grep is a commonly used command. Its most important function is to compare strings and print the strings that meet your needs. Grep selects data in the unit of "row" when searching for a string in the data. Only the rows that meet the conditions are displayed, and those that do not meet the conditions are not displayed.

1. Search for a specific string

For example

# Grep-n 'the myfile.txt

Find the word in myfile.txt and display the row number.

# Grep-vn 'the myfile.txt

Find the word in myfile.txt, And the row without the word is displayed.

# Grep-in the 'myfile.txt

Regardless of the case, the upper and lower case are the same, and the rows with the same name are displayed.

2. Use [] to search for collection characters

If you want to search for the words "test" and "taste", you can find that they share the same part, that is,'t? St', at this time, we can search like this

# Grep-n't [AE] st'myfile.txt

In fact, there are only a few characters in []. Therefore, the above example shows that we only need two strings, tast or test.

To search for characters with aa, use the following method:
# Grep-n 'A' myfile.txt

However, if you do not want to have c in front of aa, you can use [^] to reverse select the character set:
# Grep-n' [^ c] a' myfile.txt

Of course, you can also write in [], for example, only lowercase letters can appear before abc.
# Grep-n' [a-z] abc 'myfile.txt

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.