In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Linux's grep regular expression example". In daily operation, I believe many people have doubts about Linux's grep regular expression example. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Linux grep regular expression example". Next, please follow the editor to study!
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:
The code is as follows:
^ W1
W1 | W2
[^]
Grep regular expression example
Search the / etc/passswd directory for 'vivek'
The code is as follows:
Grep vivek / etc/passwd
Examples of output:
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 vivek with arbitrary case (that is, case-insensitive search)
The code is as follows:
Grep-I-w vivek / etc/passwd
Search for vivek or raj with arbitrary case
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 to regularly match the beginning or end of the input line, respectively. The following example search shows input lines that only start with vivek:
The code is as follows:
Grep ^ vivek / etc/passwd
Examples of output:
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 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. )
The code is as follows:
Grep-w ^ vivek / etc/passwd
Find the line that ends with the word word:
The code is as follows:
Grep 'foo$' filename
Matches only lines that contain only foo:
The code is as follows:
Grep'^ foo$' file name
The following example can search for blank lines:
The code is as follows:
Grep'^ $'file name
Character class
Match Vivek or vivek:
The code is as follows:
Grep'[vV] ivek' file name
Or
The code is as follows:
Grep'[vV] [iI] [Vv] [Ee] [kK] 'filename
You can also match numbers (that is, match vivek1, Vivek2, and so on):
The code is as follows:
Grep-w'[vV] ivek [0-9] 'filename
Can match two numeric characters (that is, foo11, foo12, and so on):
The code is as follows:
Grep 'foo [0-9] [0-9]' filename
Not only limited to numbers, but also able to match at least one letter:
The code is as follows:
Grep'[A-Za-z] 'file name
Displays all lines that contain the "w" or "n" characters:
The code is as follows:
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 character
[: alpha:]-alphabetic character
[: blank:]-empty characters: space keys and tabs
[: digit:]-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 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:
The code is as follows:
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":
The code is as follows:
Grep'\ 'filename
Here,
\
< 匹配单词前面的空字符串 \>Match the empty string after the word
Print out all lines with only two characters:
The code is as follows:
Grep'^.. $'filename
Displays lines that begin with a dot and a number:
The code is as follows:
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)
The code is as follows:
Grep '192.168.1.254' / etc/hosts
All three dot 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 Note: in fact, the regular expression is not accurate because of the range of numbers in the IP address.)
The code is as follows:
Egrep'[[: digit:]] {1 digit 3}\. [: digit:] {1 digit 3}\. [[: digit:]] {1 digit 3}\. File name [: digit:]
How do I search for matching patterns that start with a "-" symbol?
To use the-e option to search for a matching'- test--' string, if you do not use the-e option, the grep command attempts to parse'--test--' as its own option parameter:
The code is as follows:
Grep-e'--test--' file name
How to use the "or" match of grep?
Use the following syntax:
The code is as follows:
Grep-E 'word1 | word2' file name
Or
The code is as follows:
Egrep 'word1 | word2' file name
Or
The code is as follows:
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'
The code is as follows:
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:
The code is as follows:
{N}
{N,}
{min,max}
To match the character "v" appears twice:
The code is as follows:
Egrep "v {2}" file name
The following commands match "col" and "cool":
The code is as follows:
Egrep'co {1Pert 2} l' filename
The following command will match all lines with at least three'c 'characters.
The code is as follows:
Egrep'c {3,} 'filename
The following example matches the mobile phone number in the format 91-1234567890 (that is, two digits-ten digits).
The code is as follows:
Grep "[[: digit:]]\ {2\} [-]\? [[: digit:]]\ {10\}" file name
How do I highlight the grep command?
Use the following syntax:
The code is as follows:
Grep-- color regular expression file name
How to display only the matching characters, not the matching lines?
Use the following syntax:
The code is as follows:
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 after the word.
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 use backslash versions of\?,\ +,\ {,\ |,\ (and\) instead. 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.
At this point, the study on the "grep regular expression example of Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.