The regular expression used for grep in bash, bashgrep

Source: Internet
Author: User
Tags grep regular expression

The regular expression used for grep in bash, bashgrep

Regular Expressions are expressions used to match texts. They are often used to express search conditions in grep commands. In fact, it is equivalent to setting some advanced requirements when you use Baidu search. For example, if you want to find a picture of a person called James from Baidu, you will find a lot of James from the Cavaliers, in this case, you need advanced requirements: To search James, except LeBron. The regular expression is used here.

Learning regular expressions is simply a non-human task, because it does not work according to normal people's thinking, so be prepared.

I first read a lot of regular expression usage and later found that regular expressions are different in different environments, this is reflected in the fact that some words do not need escape characters \ or metacharacters. Therefore, here we integrate the content of the regular expression used for grep in bash.

Basic usage of text search tool grep

Function: Search for strings in each row of the input file.

Basic usage:

Grep [-acinv] [-color = auto] [-A n] [-B n] 'search string' file name

Parameter description:

-A: Process binary files in text format.

-C: displays the number of matching times.

-I: Case sensitivity differences are ignored.

-N: displays the row number at the beginning of the row.

-A: After indicates that data in n rows After matching strings is displayed.

-B: before indicates that the data of the first n rows matching the string is displayed.

-V: show the meaning of no matching row-A: After, show the meaning of n rows-B: before After the matching part, show n rows before the matching part

-Color: highlight matching keywords in a specific color

Text Search Tool grep Regular Expression metacharacters

^

For example, '^ grep' matches all rows starting with grep.

$

For example, 'grep $ 'matches all rows ending with grep.

.

Match a non-linefeed character, for example, 'gr. P' matches gr followed by any character and then p.

*

Match zero or multiple previous characters, for example, 'grep' matches all one or more spaces followed by the grep line. . Represents any character.

[]

Matches a character in a specified range, for example, '[Gg] rep' matches Grep and grep.

[^]

Match a character that is not within the specified range, such as '[^ A-FH-Z] rep' match a letter that does not start with the A-R and T-Z, followed by the rep line.

(..)

Mark matching characters, such as '(love)'. love is marked as 1.

\ <

Anchor specifies the start of a word, for example :'\

The above is the regular expression in bash introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time. Thank you very much for your support for the help House website!

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.