A detailed description of the regular expression in the Linux grep command

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

In Linux, Unix-like systems, how do I use the regular expression of the Grep command?

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

Regular expressions

A regular expression is a pattern used to match each line of input, which refers to a sequence of characters. Here's an example:

^w1w1|w2[^]
grep Regular Expression Example

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

grep vivek/etc/passwd

Output Example:

Vivek:x:1000:1000:vivek gite,,,:/home/vivek:/bin/bashvivekgite:x:1001:1001::/home/vivekgite:/bin/shgitevivek:x : 1002:1002::/home/gitevivek:/bin/sh

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

Grep-i-W VIVEK/ETC/PASSWD

Search for Vivek or Raj of any size

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 searches for input lines that only start with Vivek:

grep ^vivek/etc/passwd

Output Example:

Vivek:x:1000:1000:vivek gite,,,:/home/vivek:/bin/bashvivekgite:x:1001:1001::/home/vivekgite:/bin/sh

You can only search for the line beginning with the word Vivek, that is, do not show vivekgit, VIVEKG, etc. (LCTT: The word is followed by spaces, symbols and other English word separators. )

Grep-w ^vivek/etc/passwd

Find the line that ends with Word word:

grep ' foo$ ' file name

Match rows that contain only foo:

grep ' ^foo$ ' file name

The following example can search for a blank line:

grep ' ^$ ' file name
Character class

Match Vivek or Vivek:

grep ' [vv]ivek ' filename

Or

grep ' [vv][ii][vv][ee][kk] ' file name

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

Grep-w ' [vv]ivek[0-9] ' file name

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

grep ' foo[0-9][0-9] ' file name

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

grep ' [a-za-z] ' file name

Displays all lines that contain the "w" or "N" characters:

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:]– Space character: tab, line break, Vertical tab, page feed, carriage return, and space key
    • [: 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:

grep ' [: Upper:] ' file name
Wildcard characters

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

grep '/<b.t/> ' file name

Over here

    • /< matches an empty string before a word
    • /> matches an empty string after a word

Print out all lines with only two characters:

grep ' ^. $ ' File name

Displays a line that begins with a dot and a number:

grep ' ^/. [0-9] ' file name '
Point character Escapes

The following match to the 192.168.1.254 of the IP address is not correct: (LCTT: Can match to the IP address, but it is also possible to match the interval symbol is not a point similar format)

grep ' 192.168.1.254 '/etc/hosts

All three point characters need to be escaped:

grep ' 192/.168/.1/.254 '/etc/hosts

The following example can only match the IP address: (LCTT: In fact, due to the range of numbers in the IP address, the regular expression is not accurate)

Egrep ' [[:d igit:]]{1,3}/. [[:d Igit:]] {1,3}/. [[:d Igit:]] {1,3}/. [[:d Igit:]] {1,3} ' file name
How do I search for matching patterns that begin with the "-" symbol?

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

Grep-e '--test--' file name
How do I use grep's "or" match?

Use the following syntax:

Grep-e ' word1|word2 ' filename or egrep ' word1|word2 ' file name

or a

grep ' word1/|word2 ' file name
How do I use Grep's "and" matches?

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

grep ' word1 ' file name | grep ' Word2 '
How do I use sequence detection?

Using the following syntax, you can detect the number of occurrences of a character in a sequence:

N {N,} {Min,max}

To match the character "V" appears two times:

Egrep "v{2}" file name

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

Egrep ' co{1,2}l ' file name

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

Egrep ' c{3,} ' file name

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

grep "[[:d igit:]]/{2/}[-]/? [[:d igit:]]/{10/} ' file name
How do I highlight the grep command?

Use the following syntax:

grep--color Regular Expression file name
How about just displaying the matching characters instead of matching the lines?

Use the following syntax:

Grep-o Regular Expression file name
Regular Expression Qualifier
Qualifier Description
. Match any one of the characters.
? Matches the preceding sub-expression, up to one time.
* Matches the preceding subexpression 0 or more times.
+ Matches the preceding subexpression one or more times.
N Matches the preceding sub-expression N times.
{N,} Matches the preceding sub-expression N times to multiple times.
{N,m} Matches the preceding subexpression N to M times, at least N times up to M times.
- Indicates the range of the sequence as long as it is not at the beginning, end, or end of the sequence.
^ Matches an empty string at the beginning of a row, or a character that is not in the list to match.
$ Matches an empty string at the end of a line.
/b Matches an empty string before and after a word.
/b Matches an empty string in the middle of a word.
/< Matches the empty string in front of the word.
/> Matches the empty string following the word.
grep and Egrep

Egrep is equivalent to grep-e . It interprets patterns in the pattern of extended regular expressions. The following is a help page from grep:

Basic Regular Expression metacharacters:, +, {, |, (and) have lost their original meaning, to be used instead of the backslash version/?,/+,/{,/|,/(and/). Traditional Egrep does not support {metacharacters, some egrep implementations are/{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 the traditional usage, assuming {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 the extension of this operation, but should be avoided in portable script files.

Reference:

    • grep and Regex Help manual page (7)
    • GREP's Info page

A detailed description of the regular expression in the Linux grep command

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.