A comprehensive analysis of the usage of the regular expressions in Linux grep commands

Source: Internet
Author: User
Tags grep regular expression regular expression alphanumeric characters egrep

Linux comes with the GNU grep command tool, which supports extended regular expression extended regular expressions, and GNU grep is the default for all Linux systems. The Grep command is used to search for any information that is stored on your server or workstation.

Regular expressions

A regular expression is a pattern that matches the input of each row, and the pattern refers to a sequence of characters. Here's an example:

The code is as follows:

^w1

W1|w2

[^ ]

grep Regular Expression Example

Search for ' Vivek ' in the/ETC/PASSSWD directory

The code is as follows:

grep vivek/etc/passwd

Output Example:

The code is as follows:

Vivek:x:1000:1000:vivek gite,,,:/home/vivek:/bin/bash

Vivekgite:x:1001:1001::/home/vivekgite:/bin/sh

Gitevivek:x:1002:1002::/home/gitevivek:/bin/sh

Search for arbitrary Vivek (that is, case-insensitive search)

The code is as follows:

Grep-i-W VIVEK/ETC/PASSWD

Search Vivek or Raj with arbitrary capitalization

The code is as follows:

Grep-e-i-w ' Vivek|raj '/etc/passwd

The last example above shows the pattern of an extended regular expression.

Anchor Point

You can use the ^ and $ symbols separately to match the start or end of the input line. The following example search shows the input lines starting with Vivek only:

The code is as follows:

grep ^vivek/etc/passwd

Output Example:

The code is as follows:

Vivek:x:1000:1000:vivek gite,,,:/home/vivek:/bin/bash

Vivekgite:x:1001:1001::/home/vivekgite:/bin/sh

You can only search out the lines starting with the word Vivek, that is, not showing vivekgit, VIVEKG, etc. (LCTT: The word is followed by a space, a symbol, and other English word separators.) )

The code is as follows:

Grep-w ^vivek/etc/passwd

Find the line that ends with Word word:

The code is as follows:

grep ' foo$ ' filename

Matches only the rows that contain foo:

The code is as follows:

grep ' ^foo$ ' filename

You can search for blank lines as shown in the following example:

The code is as follows:

grep ' ^$ ' filename

Character class

Match Vivek or Vivek:

The code is as follows:

grep ' [vv]ivek ' filename

Or

The code is as follows:

grep ' [VV][II][VV][EE][KK] ' filename

You can also match numbers (that is, matching vivek1 or VIVEK2, and so on):

The code is as follows:

Grep-w ' [vv]ivek[0-9] ' filename

Can match two numeric characters (i.e. foo11, foo12, etc.):

The code is as follows:

grep ' foo[0-9][0-9] ' filename

It is not limited to numbers, it can also match at least one letter:

The code is as follows:

grep ' [a-za-z] ' filename

Display all lines that contain "w" or "N" characters:

The code is as follows:

grep [WN] File name

The expression enclosed in parentheses, the name of the character class that is wrapped between "[:" and ":]", which represents a list of all characters belonging to this class. The standard character class names are as follows:

[: Alnum:]-alphanumeric characters

[: Alpha:]-alphabetic characters

[: blank:]-null character: Empty Gell and Tab

[:d igit:]-Number: ' 0 1 2 3 4 5 6 7 8 9 '

[: Lower:]-lowercase letters: ' A b c d e F g h i j k l m n o p q R S t u v w x y z '

[: Space:]-whitespace characters: tabs, line breaks, vertical tabs, page breaks, carriage returns, and space keys

[: Upper:]-Capital letter: ' A B C D E F G H I J K L M N O P Q R S T U V W X Y Z '

In this example, the matching of all uppercase letters is shown:

The code is as follows:

grep ' [: Upper:] ' filename

Wildcard characters

You can use "." to match a single character. The example matches a 3-character word that begins with "B" at the end of "T":

The code is as follows:

grep '

Over here

< match the empty string before the word

> match the empty string after the word

Print out all lines with only two characters:

The code is as follows:

grep ' ^.. $ ' filename

Display lines that begin with a single point and a number:

The code is as follows:

grep ' ^. [0-9] ' filename

Dot character escape

It is incorrect to match the regular formula to the IP address 192.168.1.254: (LCTT can be matched to the IP address, but it may also match to a similar format where the spacer symbol is not a point)

The code is as follows:

grep ' 192.168.1.254 '/etc/hosts

All three-point characters need to be escaped:

The code is as follows:

grep ' 192.168.1.254 '/etc/hosts

The following example can only match the IP address: (LCTT) The regular expression is not accurate due to the range of digits in the IP address.

The code is as follows:

Egrep ' [[:d igit:]]{1,3}. [[:d Igit:]] {1,3}. [[:d Igit:]] {1,3}. [[:d Igit:]] {1,3} ' filename

How do I search for a matching pattern that starts with a "-" symbol?

To search for a matching '--test--' string using the-e option, if the-e option is not used, the grep command attempts to parse '--test--' as its own option argument:

The code is as follows:

Grep-e '--test--' filename

How do I use grep's "or" match?

Use the following syntax:

The code is as follows:

Grep-e ' word1|word2 ' filename

Or

The code is as follows:

Egrep ' word1|word2 ' filename

Or is

The code is as follows:

grep ' word1|word2 ' filename

How do I use Grep's "and" match?

Use the following syntax to display all rows that contain both ' word1 ' and ' Word2 '

The code is as follows:

grep ' word1 ' filename | grep ' Word2 '

How do I use sequence detection?

You can detect the number of occurrences of a character in a sequence by using the following syntax:

The code is as follows:

N

{N,}

{Min,max}

To match the character "V" appears two times:

The code is as follows:

Egrep "V{2}" filename

The following commands can be matched to "col" and "cool":

The code is as follows:

Egrep ' co{1,2}l ' filename

The following command will match all rows with at least three ' C ' characters.

Copy Code

The code is as follows:

Egrep ' c{3,} ' filename

The following example matches the phone number of 91-1234567890 (that is, two digits-10 digits) in this format.

Copy Code

The code is as follows:

grep [[:d igit:]]{2}[-]? [[:d Igit:]] {10} ' filename

How do I highlight the grep command?

Use the following syntax:

Copy Code

The code is as follows:

grep--color Regular Expression file name

What about just showing matching characters instead of matching out rows?

Use the following syntax:

Copy Code

The code is as follows:

Grep-o Regular Expression file name

Regular Expression Qualifier

qualifier description
*
+
{n}
{n,} matches the preceding subexpression N times To many times.
{n,m}
As long as it is not at the beginning, end, or end of the sequence, the range of the sequence is represented.
^
$
b
b
<
>

grep and Egrep

Egrep equal to GREP-E. It interprets patterns in the pattern of extended regular expressions. The following help pages from grep:

Basic regular expression meta characters?, +, {, |, (and) have lost their original meaning, use a backslash version, +, {, |, (and) instead. The traditional egrep does not support {metacharacters, some egrep implementations are in {substituted, so a portable script should avoid using the {symbol in GREP-E, to match the literal {should use [}].

The GNU GREP-E attempts to support traditional usage, and it assumes that {is not a special character before the invalid interval specification string.

For example, the grep-e ' {1 ' command searches for a string containing {12 characters without reporting a regular expression syntax error.

The POSIX.2 standard allows for an extension of this operation, but should be avoided in portable script files.

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.