1. Introduction to Grep
grep (Global search regular Expression_r (RE) and print out of the line, full search of regular expressions and print out the lines) is a powerful text search tool that uses regular expressions to search for text, and print out the matching lines. The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is the extension of grep, which supports more re metacharacters, and fgrep is fixed grep or fast grep, which regards all the letters as words, that is, the metacharacters in the regular expression represents the literal meaning back to itself, no longer special. Linux uses the GNU version of grep. It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options.
grep works in such a way that it searches for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to the screen without affecting the contents of the original file.
grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work.
2. grep regular expression meta-character set (base set)
-
- ^
-
-
The start of the anchoring line is as follows: ' ^grep ' matches all lines that begin with grep.
-
- $
-
-
The end of the anchoring line is as follows: ' grep$ ' matches all rows ending with grep.
-
- .
-
-
Match a non-newline character such as: ' GR.P ' matches gr followed by an arbitrary character followed by P.
-
- *
-
-
Match 0 or more previous characters such as: ' *grep ' matches all one or more spaces followed by the grep line. * Use together to represent any character.
-
- []
-
-
Matches a specified range of characters, 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 contain a-r and t-z, immediately following the line of the Rep.
-
- \(.. \)
-
-
Tag matching characters, such as ' \ (love\) ', Love is marked as 1.
-
- \<
-
-
Anchors the beginning of a word, such as: ' \<grep ' matches a line containing a word that begins with grep.
-
- \>
-
-
Anchors the end of a word, such as ' grep\> ', that matches a line containing a word that ends with grep.
-
- X\{m\}
-
-
Repeat characters x,m times, such as: ' 0\{5\} ' matches rows containing 5 O.
-
- X\{m,\}
-
-
Repeat the character X, at least m times, such as: ' O\{5,\} ' matches at least 5 rows of O.
-
- X\{m,n\}
-
-
Repeat the character X, at least m times, not more than n times, such as: ' O\{5,10\} ' matches the line of 5--10 O.
-
- \w
-
-
Match literal and numeric characters, that is, [a-za-z0-9], such as: ' G\w*p ' matches with G followed by 0 or more literal or numeric characters, followed by P.
-
- \w
-
-
\w the inverse of the form, matching one or more non-word characters, such as the dot period and so on.
-
- \b
-
-
The word lock, such as: ' \bgrep\b ' only matches grep.
3. Meta-character expansion set for Egrep and GREP-E
- +
-
Matches one or more previous characters. such as: ' [a-z]+able ', matching one or more lowercase letters followed by able strings, such as loveable,enable,disable, etc.
- ?
-
Matches 0 or more previous characters. such as: ' Gr?p ' matches the GR followed by one or no characters, then the line of P.
- A|b|c
-
Match A or B or C. such as: grep|sed matching grep or sed
- ()
-
Grouping symbols, such as: Love (able|rs) ov+ match loveable or lovers, matching one or more ov.
- X{m},x{m,},x{m,n}
-
function with x\{m\},x\{m,\},x\{m,n\}
4. POSIX character class
POSIX (The Portable Operating System Interface) adds special character classes to the character encodings in different countries, such as [: Alnum:] is another notation for a-za-z0-9. You can place them in the [] number to be a regular expression, such as [a-za-z0-9] or [[: Alnum:]]. grep under Linux supports POSIX character classes in addition to Fgrep.
- [: Alnum:]
-
Literal numeric characters
- [: Alpha:]
-
Literal characters
- [:d Igit:]
-
numeric characters
- [: Graph:]
-
Non-null characters (non-whitespace, control characters)
- [: Lower:]
-
lowercase characters
- [: Cntrl:]
-
Control characters
- [:p rint:]
-
Non-null characters (including spaces)
- [:p UNCT:]
-
Punctuation
- [: Space:]
-
All whitespace characters (new lines, spaces, tabs)
- [: Upper:]
-
Uppercase characters
- [: Xdigit:]
-
hexadecimal digits (0-9,a-f,a-f)
5. grep command Options
-
- -?
-
-
Show matching rows up and down at the same time? Line, such as: grep-2 pattern filename Displays the top and bottom 2 rows of a matching row.
-
- -b,--byte-offset
-
-
Prints the block number where the line is printed before the matching line.
-
- -C,--Count
-
-
Only the number of matched rows is printed and the matching content is not displayed.
-
- -F File,--file=file
-
-
Extract the template from the file. An empty file contains 0 templates, so nothing matches.
-
- -h,--no-filename
-
-
When searching multiple files, the matching file name prefix is not displayed.
-
- -i,--ignore-case
-
-
ignores case differences.
-
- -q,--quiet
-
-
Cancels the display, returning only the exit status. 0 indicates that a matching row was found.
-
- -l,--files-with-matches
-
-
Prints a list of files that match the template.
-
- -l,--files-without-match
-
-
Prints a list of files that do not match the template.
-
- -n,--line-number
-
-
Prints the line number in front of the matching line.
-
- -s,--silent
-
-
does not display an error message about a file that does not exist or cannot be read.
-
- -v,--revert-match
-
-
Anti-retrieval, displaying only rows that do not match.
-
- -w,--word-regexp
-
-
If referenced by \< and \>, the expression is searched as a single word.
-
- -v,--version
-
-
Displays software version information.
6. Example
To use a good grep this tool, in fact, is to write a regular expression, so here does not have all the functions of grep to explain, only a few examples, explain a regular expression of the wording.
- $ ls-l | grep ' ^a '
-
Filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with a.
- $ grep ' test ' d*
-
Displays all rows that contain test in a file that begins with D.
- $ grep ' test ' AA bb cc
-
Displays the line that matches test in the aa,bb,cc file.
- $ grep ' [a-z]\{5\} ' AA
-
Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.
- $ grep ' w\ (es\) t.*\1 ' AA
-
If West is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have to escape the "\" number, directly written as ' W (es) t.*\1 ' on it.
1. Parameters:
-I: Ignore case
-C: Print the number of matching rows
-L: Find included matches from multiple files
-V: Find rows that do not contain matches
-N: Print row and row labels that contain matches
2, RE (regular expression)
\ ignore the original meaning of special characters in regular expressions
^ matches the start line of the regular expression
$ matches the end line of a regular expression
\< starting from the line that matches the regular expression
\> to the end of the line that matches the regular expression
[] A single character, such as [a] that a meets the requirements
[-] range, such as [A-z], i.e. a,b,c to Z all meet the requirements
. All the individual characters
* All characters, length can be 0
3. Example
# Ps-ef | grep in.telnetd
Root 19955 181 0 13:43:53? 0:00 in.telnetd
# more Size.txt The contents of the size file
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# More Size.txt | grep ' [A-b] ' range, such as [A-z], i.e. a,b,c to Z all meet the requirements
b124230
b034325
a081016
a022021
a061048
b103303
A013386
b044525
# More Size.txt | grep ' [A-b] ' *
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# More Size.txt | grep ' [b] ' single character; if [a] is a compliance requirement
b124230
b034325
b103303
b044525
# More Size.txt | grep ' [BB] '
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345
# grep ' Root '/etc/group
Root::0:root
Bin::2:root,bin,daemon
Sys::3:root,bin,sys,adm
Adm::4:root,adm,daemon
Uucp::5:root,uucp
Mail::6:root
Tty::7:root,tty,adm
Lp::8:root,lp,adm
Nuucp::9:root,nuucp
Daemon::12:root,daemon
# grep ' ^root '/etc/group matches the start line of the regular expression
Root::0:root
# grep ' UUCP '/etc/group
Uucp::5:root,uucp
Nuucp::9:root,nuucp
# grep ' \<UUCP '/etc/group
Uucp::5:root,uucp
# grep ' root$ '/etc/group matches the end line of the regular expression
Root::0:root
Mail::6:root
# More Size.txt | Grep-i ' B1. '-I: Ignore case
b124230
b103303
B103303
# More Size.txt | Grep-iv ' B1. '-V: Find rows that do not contain matches
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
A013386
b044525
m8987131
B081016
M45678
BADc2345
# More Size.txt | Grep-in ' B1. "
1:b124230
9:b103303
15:b103303
# grep ' $ '/etc/init.d/nfs.server | Wc-l
128
# grep ' \$ '/etc/init.d/nfs.server | Wc–l ignores the original meaning of special characters in regular expressions
15
# grep ' \$ '/etc/init.d/nfs.server
Case "$" in
>/tmp/sharetab.$$
["x$fstype"! = Xnfs] &&
echo "$path \t$res\t$fstype\t$opts\t$desc"
>>/tmp/sharetab.$$
/usr/bin/touch-r/etc/dfs/sharetab/tmp/sharetab.$$
/usr/bin/mv-f/tmp/sharetab.$$/etc/dfs/sharetab
If [-f/etc/dfs/dfstab] &&/usr/bin/egrep-v ' ^[]* (#|$) '
If [$STARTNFSD-eq 0-a-f/etc/rmmount.conf] &&
If [$startnfsd-ne 0]; Then
elif [!-n "$_init_run_level"]; Then
While [$wtime-GT 0]; Do
Wtime= ' Expr $wtime-1 '
If [$wtime-eq 0]; Then
echo "Usage: $ {Start | Stop} "
# more Size.txt
The test file
Their is files
The end
# grep ' the ' size.txt
The test file
Their is files
# grep ' \<the ' size.txt
The test file
Their is files
# grep ' the\> ' size.txt
The test file
# grep ' \<the\> ' size.txt
The test file
# grep ' \<[tt]he\> ' size.txt
The test file
The end
For example:
1 #!/bin/sh2User=Test_user3group=Test_group4 5 #Create group if not exists6Egrep"^ $group"/etc/group >&/dev/NULL7 if[ $?-ne0 ] 8 Then9Groupadd$group Ten fi One A #Create user if not exists -Egrep"^ $user"/etc/passwd >&/dev/NULL - if[ $?-ne0 ] the Then -Useradd-g$group $user -Fi
Egrep Find if a user group exists
The use of grep and Egrep in Linux