Linux Regular Expressions and grep commands

Source: Internet
Author: User

Linux Regular Expressions and grep commands

In linux, because everything is a file, text operations account for a large proportion. Regular Expressions are a powerful tool for processing text.

Regular expressions are used to recognize text. They are similar to shell wildcards that match files or pathnames. Many command line tools support regular expressions. grep is the most commonly used tool.

Grep usage

Grep search_phrase filename

Find the file containing search_phrase in the file indicated by filename. The main options of grep are as follows:

Option Function
-I Case Insensitive. The query text is case insensitive.
-V Does not match. In general, grep prints the file that contains the query text, and adds the modification option to print the file that does not contain the text.
-C Print the number of matching files
-L Print the file name that contains the query text, instead of the text line. The default value is text line.

Note: grep searches based on the file content and matches each row of the file according to the specified pattern. When used in combination with find, grep is also searched in the content of the file output by the find command.

Regular Expression

Regular Expressions and shell wildcards also contain many special characters. All except these special characters are original characters.

Special characters

Character Function
^ 1. ^ indicates the beginning of the text when starting with a string
2. ^ when the first position in [], it indicates no. When it is another position, it is a common character.
$ 1. End of character
2. Get the variable value
. Any single character
[] Any single character in the brackets. special characters do not have special meanings in [] Except ^ and-at the specified position.
{} Matches a specified number of times (repeated ). In bash, the command set or directory can be separated.
- The range is not the first or last position in. Specific values are related to specific language families.
? 0 or once (repeated)
* Repeat multiple times ). Any string of any length in bash.
+ Repeated at least once)
() Group character. In bash, it indicates the command specified by the sub-shell.
| Logic or. Indicates the MPs queue in bash.
\ Special Character escape character.

When we pass a regular expression that contains metacharacters in bash, it is critical to enclose special characters in single quotes to prevent shell from trying to expand them.

The following describes the relationship between the language family and the range of the regular expression:

A regular expression is used to process the expression of a string. The language family configuration that has an impact on character sorting will affect the regular expression.

For example, the English and numbers are encoded in en_US.utf8 and C as follows:
LANG = C: 0 1 2 3 4... a B c d... Z a B c d... z
LANG = en_US.utf8: 0 1 2 3 4... a A B B c C d... z Z z

From the above, we can find that the character sorting in different languages is different. If you want to obtain uppercase characters while using [A-Z], you will find that LANG = C (or POSIX) is a sequential uppercase character.


However, if en_US.utf8 is used, all letters except lowercase a will be retrieved. Because the sorting order is [A B B c C d D... z Z ].


Therefore, when using this expression, you must pay special attention to the context of the current environment. Otherwise, you will find that the obtained results are different from those of others.

Modify a language family <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + cost + 00 +/hour + hour/go8cD48L3A + hour + 1 ttTV/dTyse2078q9o6zL + hour = "http://www.2cto.com/uploadfile/Collfiles/20150403/2015040310001813.jpg" alt = "">

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.