Use Linux Regular expressions to search for text in a file _ regular expressions

Source: Internet
Author: User

A regular expression is a symbolic representation that is used to identify text patterns. The main program for Linux to process regular expressions is grep. grep searches for rows that match regular expressions and transports the results to standard output.

1. grep matching mode

grep accepts options and parameters (where a regex represents a regular expression) as follows

Copy Code code as follows:

grep [Options] regex [Files]

The options are mainly the following table:

Options Meaning Function description
-I. Ignore case Ignore case
-V Invert match Does not match a matching
-L File-with-match Output matching filename
-L File-without-match Output mismatched filename
-C Count Output match number (number of rows)
-N Number Output matching rows with the preceding file name plus the number of rows in the file name
-H No-filename Suppresses the output of file names

As a Linux administrator, you need to work with text files. You can use different tools such as grep, awk, and sed to find files that contain specific text strings. Here, I'll introduce a way to use regular expressions to search for text in a file in a flexible way.

Let's consider an example of how a regular expression works. For example, when you try to use the command Grep–r host/, the result of the return will be huge. Because every word that contains the text string of the host (such as Ghostscript) will match. With regular expressions, you can better customize what you are looking for. For example, you can use the regular expression ' ^host ' to tell grep to find only the rows that start with host.

Regular expressions are not available in all commands, and the commands you use must already be programmed so that regular expressions can be used. The most commonly used commands in these commands are grep, TR, and VI. Other tools, like sed and awk, can also use regular expressions.

One example of using regular expressions is:

Copy Code code as follows:

grep ' lin.x ' *

The point in the regular expression ' lin.x ' has a special meaning. It matches any character that is in that position. To prevent interpretative problems, I recommend that you always place a regular expression between single quotes, which prevents the shell from interpreting the regular expression.

  Using regular expressions

You can do a lot of things with regular expressions. In the following list, you can find examples of some of the most common and commonly used regular expressions.

* ^: Indicates that a text string must be at the beginning of a line. So, when you find rows that start with only "hosts," You can use the command: Grep-ls ' ^hosts '
* $: Represents the end of a line. So, when you find rows that end with only "hosts," You can use the command: Grep-ls ' hosts$ '
* You can combine ^ and $ in a regular expression to find rows that contain only "yes", using the command Grep-ls ' ^yes$ '
*.: A wildcard that can refer to any character other than a line break. To find rows that contain Tex, Tux, Tox, or tix, you can use: Grep-ls ' t.x '
* []: Indicates that the characters between square brackets are optional in a regular expression. To find a user named Pinda or Linda, you can use the command: Grep-ls ' [Pl]inda '
* [^]: ignores all characters after the ^ in square brackets. To find the row containing the text Linda, and ignore the line containing only Linda or Pinda, the command is: Grep-ls ' [^pl]inda '
*-: Represents a class or a range of characters. This is useful in commands such as tr. The following command converts all lowercase letters to uppercase: TR-Z-z < mytext. Similarly, you can use regular Expressions Grep-ls ' ^0-9 ' to find some of the rows that start with numbers.
* \< and \&gt: Find patterns at the beginning or end of a word. The word that starts with a "SAN" is the command for the start of the lookup line: grep \<san< code>. These regular expressions have two drawbacks-they do not look for rows that begin with the specified regular expression, and they are not supported by all the tools. However, VI and grep support this usage.
* \: Ensure that characters with special meanings in regular expressions are not interpreted. Find the command for a text string that starts with any character followed by the text "host" as Grep-ls '. Host '. And if you need to find a text string with the dot first character followed by "host", you can use the command: Grep-ls ' \.host '

These regular expressions can help you find words that contain a specific string of text. You can also use regular expressions to specify how often the string appears in a word. For example, you can use regular expressions to search for files that contain exactly the user name "Linda" three times. To do this, you need to use the repeating operator of the regular expression and guarantee that the entire regular expression is in quotation marks. Without quotes, you may cause the shell to interpret your duplicate operators.

  The list of most important repeat operators is as follows:

* *: Indicates that the preceding regular expression may occur once, multiple times, or not at all. Note: Do not confuse with the * in the shell-in the shell, * represents any character, and in the regular expression, * indicates that the previous regular expression may exist.
*?: Indicates that the location may be a character (but not necessarily). For example, the command to find both color and colour is: Grep-ls ' COLO.R '
* +: Indicates that the preceding character or regular expression must appear at least once
* \{n\}: Indicates that the preceding character or regular expression must appear at least n times. This is useful when you look up a number between 100 and 999: Grep-ls ' 0-9\{3\} '

We have provided you with an overview of how regular expressions are used. This allows you to be more efficient when you are an administrator. Regular expressions can provide more functionality, including some rather complex operations. But before you do, first get the skills that we've covered. Regular expressions can be so complicated that you can easily get lost.

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.