Introduction to grep regular expressions in Linux system

  

Linux system regular expressions are string description methods. The main regular expressions are sed, vim, awk, and of course grep regular expressions. In the previous article, we have introduced the use of regular expressions in Linux systems. Today we will focus on the grep regular expressions in Linux. Start

^ anchor line such as::

grep regular expression metacharacters set & lsquo; ^ grep & rsquo; beginning with grep to match all the rows.

$ End of anchor line For example: ‘grep$’ matches all lines ending with grep.

. Match a non-line break character such as: & lsquo; gr.p & rsquo; match gr followed by an arbitrary character, then p.

* Matches zero or more previous characters such as: ‘*grep’ matches all one or more spaces followed by grep. .* Use together to represent 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& rsquo; matches a letter that does not contain A-R and T-Z at the beginning, followed by the rep line.

/(../) Marks matching characters, such as ‘/(love/)’,love is marked as 1.

/Anchor the beginning of a word, such as: & lsquo; /match the line containing the word starting with grep.

/" Anchor the end of the word, such as & rsquo;grep /"& lsquo; match the line containing the word ending with grep.

x/{m/} Repeat the character x, m times, such as: & rsquo;0/{5/}& lsquo; match the line containing 5 o.

x/{m,/} Repeat the character x, at least m times, such as: & rsquo;o/{5, /} & lsquo; match at least 5 o lines.

x/{m,n/} repeats the character x, at least m times, no more than n times, such as: & rsquo;o/{5,10/}‘ matches 5-10 o Row.

/w matches text and numeric characters, that is, [A-Za-z0-9], such as: & rsquo;G/w*p‘ matches with G followed by zero or more literal or numeric characters, Then there is p.

/b The word locker, such as: ’/bgrep/b‘ matches only grep.

The commonly used grep options are:

-c Output only the number of matching lines.

-i is not case sensitive (only for single characters).

-h Does not display file names when querying multiple files.

-l When querying multiple files, only the file name containing the matching characters is output.

-n Displays matching lines and line numbers.

-s Does not display error messages that do not exist or have no matching text.

-v Displays all lines that do not contain matching text.

-V Display software version information

When using grep matching, it is best to use double quotes to prevent the system from mistaking parameters or special commands, or to match multiple words. Previous12Next page Total 2 pages

Copyright © Windows knowledge All Rights Reserved