Regular expression is a type of expression used to match text. It is often used to express search conditions in the grep command. Next, this article will introduce you to the regular expressions used for grep in bash. Friends who need it can refer to it
Regular expressions are a type of expression used to match text and are often used to express retrieval in the grep command. condition. In fact, it is equivalent to setting some advanced requirements when you search on Baidu. For example, if you want to find photos of a person named James on Baidu, you will find a lot of James from the Cavaliers. At this time, you need advanced requirements: Search except James besides LeBron. This is where regular expressions are used.
Learning regular expressions is simply an inhuman job, because it does not work according to normal people's thinking at all, so you must be mentally prepared.
I saw a lot of usage of regular expressions when I first started learning. Later I discovered that regular expressions are different in different environments. This is reflected in whether certain words require Escape character\ and metaCharacter set, so here we will integrate the regular expression related content for grep in bash.
Text Basic usage of search tool grep
Function: Find string in each line of the input file.
Basic usage:
grep [-acinv] [–color=auto] [-A n] [-B n] 'Search string' file name
Parameter description:
-a: Process the binary document as text
-c: Display the number of matches
-i : Ignore the case difference
-n: Display the line number at the beginning of the line
-A: After means, display the data of n lines after the matching string
-B :before means, displays the data of n lines before matching string
-v: displays no matching lines -A: After means, displays n lines after the matching part -B: before means, displays the matching part n lines before
–color: Highlight matching keywords in a specific color
Text search tool grep regular expression metacharacters set
^
The beginning of the anchor line is like: '^grep' matches all lines starting with grep.
$
End of anchor line For example: 'grep$' matches all lines ending with grep.
.
Matches a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.
*
Matches zero or more previous characters. For example: 'grep' matches all lines with one or more spaces followed by grep. . used together represents any character.
[]
Matches characters within a specified range, such as '[Gg]rep' matches Grep and grep.
[^]
Matches a character that is not within the specified range, such as: '[^A-FH-Z]rep' matches a letter that does not include A-R and T-Z, followed by rep line.
(..)
mark matching characters, such as '(love)', love is marked as 1.
\<
Anchor the beginning of the word, such as: '\
The above is the regular expression in bash introduced by the editor. I hope it will be useful to you. Everyone is helpful. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the Script House website!
The above is the detailed content of Regular expression for grep in bash_regular expression. For more information, please follow other related articles on the PHP Chinese website!