2. Use
The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.
2. Format
grep [Options]
3. Main parameters
[Options] Main parameters:
-C: Outputs only the total number of matching rows.
-I: Case insensitive (only for single-character).
-L: Only file names that contain matching characters are output when querying multiple files.
-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.
-e using extended regular expressions
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
.: all the individual characters.
*: There are characters, the length can be 0.
4.grep command uses a simple instance
$ 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.
5. By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' is listed in the following form:
Grep:sound:Is a Directory
This may make the output of ' grep ' difficult to read. There are two ways to solve this problem:
Explicit Requirements Search subdirectories: grep-r
or Ignore subdirectories: grep-d Skip
6.\< and \> each mark the beginning and end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep ' \<man\> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
grep ' \<man ' matches ' manic ' and ' man ', but not ' Batman ',
grep ' man\> ' * matches Batman, but not ' manic '
7, using the class name
You can use the class name that matches the international pattern:
[[: Upper:]] [A-z]
[[: Lower:]] [A-z]
[[:d Igit:]] [0-9]
[[: Alnum:]] [0-9a-za-z]
[[: Space:]] space or tab
[[: Alpha:]] [A-za-z]
grep ' 5[[:upper:]][[:upper:]] ' Data.doc #查询以5开头以两个大写字母结尾的行
8. Example
1) # 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, matches the $ symbol
(2) Mismatch test
grep ' ^[^48] ' Data.doc #不匹配行首是48的行
(3) Using extended mode matching
Grep-e ' 219|216 ' Data.doc
"Shell" grep command