In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use regular expressions in Grep Regex. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Grep is one of the most useful and powerful commands for text processing in Linux. Grep searches one or more input files for lines that match the regular expression and writes each matching line to standard output.
We will explore the basics of using regular expressions in the GNU version of grep, which is provided by most Linux operating systems by default.
Grep regular expression
A regular expression or regular expression is a pattern that matches a set of strings. Patterns are made up of operators, constructor characters, and metacharacters that have special meanings. GNU grep supports three kinds of regular expression syntax, Basic,Extended and Perl are compatible.
In its simplest form, grep interprets the search pattern as a basic regular expression when no regular expression type is given. To interpret the pattern as an extended regular expression, use the-E (or-extended-regexp) option.
In the grep implementation of GNU, there is no functional difference between basic regular expression syntax and extended regular expression syntax. The only difference is that in basic regular expressions, metacharacters? , +, {, |, (and) are interpreted as text characters. In order to maintain the special meaning of metacharacters when using basic regular expressions, characters must be escaped using a backslash (\). We will explain the meaning of these and other metacharacters later.
In general, you should always enclose regular expressions in single quotation marks to avoid shell interpreting and extending metacharacters.
Text matching
The most basic use of the grep command is to search for text characters or a series of characters in a file. For example, to display all lines that contain the string "bash" in the / etc / passwd file, you can run the following command:
[linuxidc @ localhost www.linuxidc.com] $grep bash / etc/passwd
The output should look like this:
Root:x:0:0:root:/root:/bin/bash
Linuxidc:x:1000:1000:linuxidc:/home/linuxidc:/bin/bash
Regular expressions in Grep (Regex)
In this example, the string "bash" is a basic regular expression consisting of four literal characters. This tells grep to search for the string "b" with "a", "s" and "h" immediately after it.
By default, the grep command is case sensitive. This means that uppercase and lowercase characters are treated as different characters. To ignore case when searching, use the-I option (or-ignore-case).
It is important to note that grep looks for search patterns as strings rather than words. Therefore, if you search for "gnu", grep will also print lines where "gnu" embeds larger words, such as "cygnus" or "magnum".
If the search string contains spaces, you need to enclose it in single or double quotation marks:
$grep "Gnome Display Manager" / etc/passwd
Anchor Anchoring
Anchoring is a metacharacter that allows you to specify where a match must be found in the line.
The ^ (caret) symbol matches an empty string at the beginning of a line. In the following example, the string "linux" is matched only if it appears at the beginning of the line.
$grep'^ linux' file.txt
The $(USD) symbol matches the empty string at the beginning of the line. To find a line that ends with the string "linux", you can use:
$grep 'linux$' file.txt
You can also use two anchors to construct a regular expression. For example, to find a row that contains only "linux", run:
$grep'^ linux$' file.txt
Another useful example is the ^ $pattern that matches all blank lines.
Match a single character
. (dot) symbols are metacharacters that match any single character. For example, to match anything that begins with "kan" and then has two characters and ends with the string "roo", you can use the following pattern:
$grep 'kan..roo' file.txt
Parenthetical expression
Square bracket expressions allow a set of characters to be enclosed in square brackets [] to match a set of characters. For example, to find a line that contains "accept" or "accent", you can use the following expression:
$grep'acce [NP] t 'file.txt
If the first character in square brackets is the caret ^, it corresponds to any single character that is not contained in square brackets. The following pattern matches any combination of strings that begin with "co", followed by any letter followed by "l" followed by "la" (such as "coca", "cobalt", and so on), but does not match the line containing "cola":
$grep'co [^ l] a 'file.txt
Instead of placing characters one by one, you can specify a series of characters in square brackets. Construct a range expression by specifying the first and last characters of a range separated by a hyphen. For example, [amura] is equivalent to [abcde], and [1-3] is equivalent to [123].
The following expression matches each line that begins with an uppercase letter:
$grep'^ [Amurz] 'file.txt
Grep also supports predefined character classes in parentheses. The following table shows some of the most common character classes:
Quantifier Character Classes
[: alnum:] alphanumeric characters.
[: alpha:] alphabetic characters.
[: blank:] spaces and tabs.
[: digit:] number.
[: lower:] lowercase letters.
[: upper:] capital letters.
For a complete list of all character categories, see the Grep manual.
Quantifier
The quantifier Quantifier allows you to specify the number of matches that must appear. The following table shows the quantifiers supported by GNU grep:
Quantifier description
Matches the previous item zero or more times.
? Match the last item zero or once.
Match the previous item one or more times.
{n} exactly match the previous item n times.
{n,} matches at least n items.
{, m} matches more than m items at most.
{n ~ m} match the previous item n to m times.
The * (asterisk) character matches the previous item zero or more times. The following will match "right", "sright", "ssright" and so on.
$grep's thanks right.'
Here is a more advanced pattern that matches all lines that begin with an uppercase letter and end with a period or comma. . * regular expressions match any number of characters:
$grep-E'^ [Amurz]. * [.] $'file.txt
? The (question mark) character makes the previous item optional and can only be matched once. The following matches both "bright" and "right". ? Characters are escaped with a backslash because we are using a basic regular expression:
$grep'b\? right' file.txt
Here is the same regular expression that uses the extended regular expression:
$grep-E'bounded right` file.txt
The + (plus) character matches the previous item one or more times. The following matches "sright" and "ssright", but not "right":
$grep-E's perfect right' file.txt
The curly braces character {} allows you to specify the exact number, the upper or lower limit, or the range within which a match must occur.
The following matches all integers between 3 and 9 digits:
$grep-E'[[: digit:]] {3pm 9} 'file.txt
Alternating Alternation
Alternation is a simple OR. The replacement operator | (pipe) allows you to specify different possible matches, either a text string or a set of expressions. This operator has the lowest priority of all regular expression operators.
In the following example, we search for all the words fatal, error, and critical that appear in the Nginx log error file:
$grep 'fatal | error | critical' / var/log/nginx/error.log
If you use an extended regular expression, you should not escape the operator |, as follows:
$grep-E 'fatal | error | critical' / var/log/nginx/error.log
Grouping
Grouping is a feature of regular expressions that allows you to group patterns together and use them as a reference. Use parentheses () to create a group.
When using a basic regular expression, you must escape the parentheses with a backslash (\).
The following example matches both "fearless" and "less". ? Quantifiers make (fear) optional:
$grep-E'(fear)? less' file.txt
Special expression of backslash
GNU grep contains several metacharacters, consisting of backslashes and regular characters. The following table shows some of the most common special backslash expressions:
Expression Description
\ b word lock
\
< 在单词开头匹配一个空字符串。 >Matches an empty string at the end of the word.
\ w matches a word.
\ s matches a space.
The following pattern will match the separate words "abject" and "object". If you embed larger words, they will not match:
$grep'\ Bao] bject\ b' file.txt
Summary
Regular expressions are used in text editors, programming languages, and command-line tools such as grep,sed and awk. When searching for text files, writing scripts, or filtering command output, it is helpful to understand how to construct regular expressions.
On how to share the regular expressions in Grep Regex here, I hope that the above content can be of some help to 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.
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.