Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions in grep

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use regular expressions in grep. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Grep is one of the tools used to process files in Linux. Grep searches the input file, finds the lines that match the regular expression, and outputs the standard output of each matching line.

Img

Regular expression

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

^ w1w1 | w2 [^]

Grep regular expression example

Search the / etc/passswd directory for 'vivek'

Grep vivek / etc/passwd

Examples of output:

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 vivek with arbitrary case (that is, case-insensitive search)

Grep-I-w vivek / etc/passwd

Search for vivek or raj with arbitrary case

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 to regularly match the beginning or end of the input line, respectively. The following example search shows input lines that only start with vivek:

Grep ^ vivek / etc/passwd

Examples of output:

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

You can just search for lines that start with the word vivek, that is, do not show vivekgit, vivekg, etc. (LCTT translation note: that is, the word is followed by spaces, symbols and other English word separators. )

Grep-w ^ vivek / etc/passwd

Find the line that ends with the word word:

Grep 'foo$' filename

Matches only lines that contain only foo:

Grep'^ foo$' file name

The following example can search for blank lines:

Grep'^ $'file name

Character class

Match Vivek or vivek:

Grep'[vV] ivek' file name

Or

Grep'[vV] [iI] [Vv] [Ee] [kK] 'filename

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

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

Can match two numeric characters (that is, foo11, foo12, and so on):

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

Not only limited to numbers, but also able to 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 parenthesized expression, the name of the character class wrapped between "[:" and ":]", represents a list of all characters that belong to this class. The standard character class names are as follows:

[: alnum:]

-alphanumeric characters

[: alpha:]

-alphabetic character

[: blank:]

-empty characters: space keys and tabs

[: digit:]

-figure:'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 characters: tabs, line feeds, vertical tabs, page feeds, carriage returns, and space keys

[: upper:]

-Capital 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'

What is shown in this example is to match all uppercase letters:

Grep'[: upper:] 'file name

Wildcard character

You can use "." To match a single character. In the example, match the three-character word that starts with "b" and ends with "t":

Grep'/ 'file name

Here,

Match the empty string before the word matches the empty string after the word

Print out all lines with only two characters:

Grep'^.. $'filename

Displays lines that begin with a dot and a number:

Grep'^ /. [0-9] 'file name

Dot character escape

It is incorrect to match the regular expression with the IP address 192.168.1.254 below: (LCTT translation note: it is possible to match the IP address, but it is also possible to match to a similar format in which the spacer sign is not a dot)

Grep '192.168.1.254' / etc/hosts

All three dot characters need to be escaped:

Grep '192 Universe. 168 Universe. 1 Universe. 254' / etc/hosts

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

Egrep'[[: digit:]] {1 digit 3} /. [: digit:] {1 digit 3} /. [[: digit:]] {1 digit 3} /. [: digit:] {1Magol 3} 'file name

How do I search for matching patterns that start with a "-" symbol?

To use the-e option to search for matching'- test-' strings, if you do not use the-e option, the grep command attempts to parse'- test-' as its own option parameter:

Grep-e'--test--' file name

How to use the "or" match of grep?

Use the following syntax:

Grep-E 'word1 | word2' file name or egrep' word1 | word2' file name

Or

Grep 'word1/ | word2' file name

How to use the "and" match of grep?

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

Grep 'word1' file name | grep' word2'

How to use sequence detection?

Using the following syntax, you can detect the number of times a character is repeated in a sequence:

{N} {N,} {min,max}

To match the character "v" appears twice:

Egrep "v {2}" file name

The following commands match "col" and "cool":

Egrep'co {1Pert 2} l' filename

The following command will match all lines with at least three'c 'characters.

Egrep'c {3,} 'filename

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

Grep "[[: digit:]] / {2 /} [-] /? [[: digit:]] / {10 /}" file name

How do I highlight the grep command?

Use the following syntax:

Grep-- color regular expression file name

How to display only the matching characters, not the matching lines?

Use the following syntax:

Grep-o regular expression file name

Regular expression qualifier

Qualifier description. Matches any character. ? Matches the previous subexpression, up to once. * matches the previous subexpression zero or more times. + matches the previous subexpression one or more times. {N} matches the previous subexpression N times. {N,} matches the previous subexpression N or more times. {Nrecoery M} matches the previous subexpression N to M times, at least N times to more than M times. -represents the sequence range as long as it is not at the beginning, end, or end point of the sequence. ^ matches the empty string at the beginning of a line; also indicates that the character is not in the list to match. $matches the empty string at the end of the 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 after the word.

Show details

Grep and egrep

Egrep is equivalent to

Grep-E

It interprets the pattern in the form of extended regular expressions. Here is the help page from grep:

The basic regular expression metacharacters?, +, (and) have lost their original meaning and are replaced by backslash versions of /?, / +, / {, /, / and /. Traditional egrep does not support {metacharacters, and some egrep implementations use / {instead, so a portable script should avoid using the {symbol in grep-E, and [] should be used to match the literal {.

GNU grep-E tries to support traditional usage and assumes that it is not a special character if it comes before an invalid interval specification string.

For example, the grep-E'{1' command searches for strings containing {1 characters without reporting regular expression syntax errors.

The POSIX.2 standard allows extensions to this operation, but this should be avoided in portable script files.

Thank you for reading! This is the end of this article on "how to use regular expressions in grep". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report