Quick introduction to Regular Expressions1. How to match the content you want to search?
A regular expression is only a pattern that matches each input line.
Search for 'vivek' in '/etc/passswd '.
grep vivek /etc/passwd
Output result case:
vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bashvivekgite:x:1001:1001::/home/vivekgite:/bin/shgitevivek:x:1002:1002::/home/gitevivek:/bin/sh
Search for 'vivek' in any situation (that is, no size difference ):
grep -i -w vivek /etc/passwd
Retrieve 'vivek' and 'l1' case-insensitive ':
grep -E -i -w 'vivek|raj' /etc/passwd
In the last example, the extended regular expression mode is used.
Fixed search content location:
You can use the ^ and $ symbols to force a regular expression to match the start or end position of a row. The following example shows the text starting with 'vivek.
grep ^vivek /etc/passwd
Output result example:
vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bashvivekgite:x:1001:1001::/home/vivekgite:/bin/sh
You can only display text lines starting with vivek. For example, it means that the start of a word such as vivekgite and vivekg is not displayed.
grep -w ^vivek /etc/passwd
Retrieve the text format ending with 'foo:
grep 'foo$' FILENAME
You can also use the following method to search for blank rows:
grep '^$' FILENAME
2. How to match specific characters?
Match 'vivek' or 'vivek ':
grep '[vV]ivek' FILENAME
Alternatively, you can:
grep '[vV][iI][Vv][Ee][kK]' FILENAME
You can match a number (for example, vivek1 or Vivek2 ):
grep -w '[vV]ivek[0-9]' FILENAME
You can match two digits (for example, foo11 and foo12 ):
grep 'foo[0-9][0-9]' FILENAME
It's not just a number. You can match letters:
grep '[A-Za-z]' FILENAME
Display All text lines containing letters w or n:
grep [wn] FILENAME
In the expressions in parentheses, the name of the character class included in "[:" and ":]" indicates a list Of all characters belonging to the class. Standard character class name:
[: Alnum:]-alphanumeric characters. [: Alpha:]-alphabetic order [: blank:]-spaces and tabs. [: Digit:]-number: '0 1 2 3 4 5 6 7 8 9 '. [: Lower:]-lowercase letter: 'a B c d e F '. [: Space:]-special characters: tab, line break, vertical tab, form feed, carriage return, and space. [: Upper:]-uppercase letter: '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 '.
In the following example, all uppercase letters are matched:
grep '[:upper:]' FILENAME
3. How do I use wildcards?
You can use "." To replace a single character. In the following example, all three words starting with the letter "B" and ending with the letter "t" are queried.
grep '\<b.t\>' FILENAME
In the above example:
\ <Match the space string at the start of a word \> match the space string at the end of a word
Search and output the results of all two letters:
grep '^..$' FILENAME
Search and display all results starting with '.' And numbers:
Grep '^ \. [0-9] 'filename escape character '.'
The following regular expression finds the IP address 192.168.1.254 and cannot obtain the expected results:
grep '192.168.1.254' /etc/hosts
All three points must be escaped:
grep '192\.168\.1\.254' /etc/hosts
The following example matches only one address:
egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' FILENAME
The words Linux or Unix are matched in case-insensitive mode:
egrep -i '^(linux|unix)' FILENAME
Explore grep Advanced Search Mode1. How to retrieve a pattern that starts?
Use the-e Option to search for all results matching '-test. Grep will try to parse '-test-' as an option:
grep -e '--test--' FILENAME
2. How to Use the OR logical operation in grep?
grep -E 'word1|word2' FILENAME### OR ###egrep 'word1|word2' FILENAME
Or you can do this.
grep 'word1\|word2' FILENAME
3. How to Use the AND logical operation in grep?
Follow the following syntax to display all results that contain the words 'word1 'and 'word2:
grep 'word1' FILENAME | grep 'word2'
Alternatively, you can:
grep 'foo.*bar\|word3.*word4' FILENAME
4. How to test the sequence?
You can use the following syntax to test the number of repetitions of a character in the sequence:
{N}{N,}{min,max}
Match the string containing two letters v:
egrep "v{2}" FILENAME
In the following example, the search file contains the string results of "col" and "cool:
egrep 'co{1,2}l' FILENAME
In the following example, match results with at least three letters c:
egrep 'c{3,}' FILENAME
The following example matches the phone number in the format of "91-1234567890" (that is, "two digits-ten digits ")
grep "[[:digit:]]\{2\}[ -]\?[[:digit:]]\{10\}" FILENAME
5. How to highlight grep output results?
Use the syntax of the following example:
grep --color regex FILENAME
6. How can I make the grep output show only the matched part instead of the whole line?
Use the syntax of the following example:
grep -o regex FILENAME
Regular Expression operator SummaryRegular Expression: Operator meaning
. Match any single character .? Match the first character 0 or 1 time. * Match the first character ≥ 0. + Match the first character ≥1. {N} matches the previous character n times. {N,} matches the previous character ≥m. {N, M} matches the previous character n to M times. -If the end point of a list or range is in the list, it indicates the range. ^ Start mark, indicating that an empty string is matched at the start position. It also indicates characters that are not in the list range. $ End tag. Matches an empty string. \ B word lock. Matches an empty string at the edge of a word. \ B matches an empty string in a non-edge position of a word. \ <Matches an empty string starting with a word. \> Matches an empty string at the end of a word.
About grep and egrepEgrep is grep-E, which interprets the pattern as an extended regular expression. The grep help document defines this as follows:
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{,\|, \(, and \). Traditional egrep did not support the { meta-character, and some egrep implementations support \{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a literal {. GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification. For example, the command grep -E '{1' searches for the two-character string {1 instead of reporting a syntax error in the regular expression. POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.
From: http://www.cyberciti.biz/faq/grep-regular-expressions
Address: http://www.linuxprobe.com/quickly-grasp-grep-command-and-regular-expression.html