You need to keep in mind that regular expressions are not the same as wildcard characters, they mean different things
Regular expressions are just a description of strings and can only be handled by combining tools that support regular expressions. This paper takes grep as an example to explain the regular expression.
grep command
Function: To find the string in each row of the input file.
Basic usage:
grep [-ACINV] [--color=auto] [-A n] [b-n] ' search string ' filename
Parameter description:
-A: Processing binary documents as text
-C: Show number of matches
-I: Ignore case differences
-N: Displays line numbers at the beginning of lines
-a:after, display data that matches n rows after a string
-b:before, display data that matches the first n rows of a string
-V: Shows the meaning of no matching line-a:after, showing the meaning of N line-b:before after match, showing the matching part before N line
--color: Highlight matching keyword in a specific color
The –color option is a great option for you to clearly match those characters. It is best to include in your own. BASHRC or. bash_profile file:
Alias Grep=grep--color=auto
After each grep search, the matching effect is highlighted automatically.
The ' search string ' is a regular expression, and note that in order to avoid the effect of the shell's metacharacters on the regular expression, enclose it in single quotes ("") and never enclose it in double quotes ("") or not.
Regular expressions are divided into basic regular expressions and extended regular expressions. Here is a brief summary of each.
Basic Regular Expressions
Regular expression learning is mainly about the learning of regular expression meta-data. The regular expression itself is not a profound thing, this article only on the basic regular expression of the metadata for a summary:
Extending regular Expressions
grep generally supports basic regular expressions, and can extend regular expressions through parameter-e support, in addition GREP provides an extended command that is called Egrep to support extended regular expressions, which is equivalent to GREP-E. Although in general, the basic regular expression is sufficient. In special cases, complex extended expressions can simplify string matching.
The extended regular expression is based on the basic regular expression, adding some meta data.
Linux under the regular expression of broad and profound, the above support summarizes the most commonly used parts, if proficient in the upper part of the regular expression can basically meet the day-to-day use of.
In addition, many Linux commands support regular expressions, such as Find,sed,awk and so on. Use regular expressions when you use the manuals that refer to these commands.
Linux grep Regular Expressions
grep regular Expression meta-character set:
^ Anchor Line start like: ' ^grep ' matches all rows beginning with grep.
$ anchor row end like: ' grep$ ' matches all rows with grep ending.
. Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P.
* Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces immediately following the grep row ... * To use on behalf of any character.
[] matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.
[^] matches a character that is not in the specified range, such as: ' [^a-fh-z]rep ' match does not contain a letter beginning with A-r and T-z, followed by the rep line.
\(.. \) tag matching characters, such as ' \ (love\) ', Love is marked as 1.
\ Anchors the beginning of a word, such as: ' Matches a line containing a word that starts with grep.
\> anchors the end of a word, such as ' grep\> ' to match a line containing a word that ends with grep.
X\{m\} repeats characters x,m times, such as: ' 0\{5\} ' matches rows containing 5 O.
X\{m,\} repeats the character x, at least m times, such as: ' O\{5,\} ' to match at least 5 o rows.
X\{m,n\} repeats the character x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches a row of 5--10 O.
\w matches literal and numeric characters, that is, [a-za-z0-9], such as: ' G\w*p ' matches with G followed by 0 or more text or number characters, followed by P.
\b Word locks, such as: ' \bgrep\b ' matches grep only.
The common grep options are:
-C outputs only the number of matching rows.
-I is case-insensitive (applies to given only).
-H does not display file names when querying multiple files.
-l outputs only file names that contain matching characters when querying multiple files.
-N Displays matching rows and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all rows that do not contain matching text.
-V Display software version information
The best way to use grep matches is to enclose them in double quotes, to prevent the system from mistaking arguments or special commands, or to match multiple words.
About matching instances:
Grep-c "Test.txt" Statistics all the lines that start with "48" characters
Grep-i "may" test.txt find all rows of "may" without case sensitivity
Grep-n "test.txt" shows line numbers, lines and line numbers that match the character "48", same as NL Test.txt |grep 48)
Grep-v "test.txt" Display output without character "48" all Lines)
grep "471" test.txt display the line where the output character "471" is located
grep test.txt display output begins with the character "48" and is the line where the TAB key is after the character "48"
grep "48[34]" test.txt display output begins with the character "48", and the third character is all rows of "3" or "4".
grep "^[^48]" Test.txt shows that the output header is not a line with the character "48"
grep [Mm]ay test.txt set Case lookup: Display output the first character that starts with "M" or "M" and ends with the character "Ay")
grep "K ... D "test.txt display output the first character is" K ", combined second is any character, the fifth character is the line of" D "
grep "[a-z][9]d" test.txt display output the first character range is "a-d", the second character is "9", and the third character is "D" of all rows
grep [35]. 1998 "Test.txt shows that the first character is 3 or 5, the 23rd character is arbitrary, all lines ending with 1998
grep "4\{2,\}" test.txt mode appearance probability lookup: Display output character "4" at least two occurrences of all rows
grep "9\{3,\}" test.txt mode appearance probability lookup: Display output character "9" at least three occurrences of all rows
grep "9\{2,3\}" test.txt mode appearance probability lookup: Show output character "9" repeat occurrences in a range, repeat 2 or 3 times all rows
Grep-n "^$" test.txt displays the line number of the output blank line
Ls-l |grep "^d" If you want to query directories in the directory list in the same: ls-d *
Ls-l |grep "^d[d]" queries all files in a directory that do not contain a directory
Ls-l |grpe "^d.....x. X "Query the collection of directories with executable permissions for other users and members of the user group
I'd like to introduce grep with regular expressions and Linux grep regular expressions.