1. effect
Linux In the system grep Command is a powerful text search tool , It can search for text using regular expressions , And put the horse Print out the matching lines .grep The full name is Global Regular Expression Print, Represents the global regular expression version , Its permission is all users .

2. Format
grep [options]

3. main parameter
[options] main parameter :
-c: Output only the count of matching lines .
-I: Don't differentiate between big A lowercase letter ( Only for single characters ).
-h: Do not display file name when querying multiple files .
-l: When querying multiple files, only the file name containing matching characters is output .
-n: Show matching lines and Line number .
-s: Do not display error messages that do not exist or do not match text .
-v: Display does not contain matching text of all lines .
pattern Main parameters of regular expression :
\: Ignore the original meaning of special characters in regular expressions .
^: Match the start line of the regular expression .
$: Match the end line of the regular expression .
\<: From matching regular expression The line of form begins .
\>: To the end of the line that matches the regular expression .
[ ]: Single character , Such as [A] namely A Meet the requirements .
[ - ]: Range , Such as [A-Z], namely A、B、C Until Z All meet the requirements .
.: All single characters .
* : Have a character , The length can be 0.

4.grep The command uses simple examples
$ grep ‘test’ d*
Show all to d The opening file contains test The line of .
$ grep ‘test’ aa bb cc
Displayed in the aa,bb,cc Match in file test The line of .
$ grep ‘[a-z]\{5\}’ aa
Show that all contain at least... Per string 5 Line of string with consecutive lowercase characters .
$ grep ‘w\(es\)t.*\1′ aa
If west Matched , be es It's stored in memory , And marked as 1, Then search for any character (.*), These characters are followed by Another one es(\1), Find and show the line . If you use egrep or grep -E, You don't have to ”\” No. to escape , Written directly ’w(es)t.*\1′ That's all right. .

5.grep The command uses complex instances
Suppose you are ’/usr/src/Linux/Doc’ Search the directory with characters strand ’magic’ The file of :
$ grep magic /usr/src/Linux/Doc/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
The file ’sysrp.txt’ Contains the string , The discussion is SysRQ The function of .
By default ,’grep’ Search current directory only . If There are many subdirectories under this directory ,’grep’ It will be listed in the following form :
grep: sound: Is a directory
This may make ’grep’ The output of is difficult to read . There are two solutions :
Search subdirectories are explicitly required :grep -r
Or ignore subdirectories :grep -d skip
If there are many When the output , You can pipe it to ’less’ Read on :
$ grep magic /usr/src/Linux/Documentation/* | less
such , You can read it more easily .

There is one caveat , You must provide a file filtering method ( Search all the files *). If you forget ,’grep’ I'll be waiting , Until the program is interrupted . If you come across a situation like this , Press <CTRL c> , Then try again .

Here are some interesting command line arguments :
grep -i pattern files : Search case insensitive . The default is case sensitive ,
grep -l pattern files : Only matching file names are listed ,
grep -L pattern files : List mismatched filenames ,
grep -w pattern files : Match only the whole word , Instead of being part of a string ( If it matches ’magic’, instead of ’magical’),
grep -C number pattern files : The matching context shows [number] That's ok ,
grep pattern1 | pattern2 files : Show match pattern1 or pattern2 The line of ,
grep pattern1 files | grep pattern2 : Show both match pattern1 Match again pattern2 The line of .

grep -n pattern files  The line number information is displayed

grep -c pattern files  You can find the total number of rows

There are also some special symbols for searching :
\< and \> Mark the beginning and the end of the word .
for example :
grep man * Will match ‘Batman’、’manic’、’man’ etc. ,
grep ‘\<man’ * matching ’manic’ and ’man’, But it's not ’Batman’,
grep ‘\<man\>’ Only match ’man’, instead of ’Batman’ or ’manic’ Wait for other strings .
‘^’: Refers to the matching string at the beginning of the line ,
‘$’: Refers to the matching string on the line tail ,

 

 

Grep command A complete collection of usage

1、 Parameters :
-I : Ignore case
-c : Print the number of matching lines
-l : Look for matches from multiple files
-v : Find a row that does not contain a match
-n: Print lines and line labels that contain matches

2、RE( Regular expressions )
\ Ignore the original meaning of special characters in regular expressions
^ Match the start line of the regular expression
$ Match the end line of the regular expression
\< Start with the line that matches the regular expression
\> To the end of the line that matches the regular expression
[ ] Single character ; Such as [A] namely A Meet the requirements
[ - ] Range ; Such as [A-Z] namely A,B,C Until Z All meet the requirements
. All single characters
* All characters , The length can be 0

3、 give an example
# ps -ef | grep in.telnetd
root 19955 181 0 13:43:53 ? 0:00 in.telnetd

# more size.txt size The content of the document
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] namely A,B,C Until 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 ; Such as [A] namely A Meet the requirements
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 Match 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 Match the end line of the regular expression
root::0:root
mail::6:root

# more size.txt | grep -i 'b1..*3' -i : Ignore case

b124230
b103303
B103303

# more size.txt | grep -iv 'b1..*3' -v : Find a row that does not contain a match

b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
a013386
b044525
m8987131
B081016
M45678
BADc2345

# more size.txt | grep -in 'b1..*3'
1:b124230
9:b103303
15:B103303

# grep '$' /etc/init.d/nfs.server | wc -l
128
# grep '\$' /etc/init.d/nfs.server | wc –l Ignore the original meaning of special characters in regular expressions

15
# grep '\$' /etc/init.d/nfs.server
case "$1" 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: $0 { start | stop }"

# more size.txt

the test file
their are files
The end

# grep 'the' size.txt
the test file
their are files

# grep '\<the' size.txt
the test file
their are files

# grep 'the\>' size.txt
the test file

# grep '\<the\>' size.txt
the test file

# grep '\<[Tt]he\>' size.txt
the test file

==================================================================

1, brief introduction
A versatile text search tool using regular expressions . This php?name=%C3%FC%C1%EE" οnclick="tagshow(event)" class="t_tag"> The order was ed One of the line editors php?name=%C3%FC%C1%EE" οnclick="tagshow(event)" class="t_tag"> command / filter :
        g/re/p -- global - regular expression - print.
The basic format
grep pattern [file...]
(1)grep Search string [filename]
(2)grep Regular expressions [filename]
Search the file for all pattern Position of appearance , pattern It can be a string to search for , It can also be a regular expression .
Be careful : It's best to use double quotation marks when entering a string to search for / When regular expressions are used for pattern matching , Note the use of single quotes

2,grep The option to
    -c Output only the count of matching lines
    -i Case insensitive ( For single character )
    -n Show matching line numbers
    -v Don't show lines that don't contain matching text, so there are lines
    -s Do not display error messages
    -E Using extended regular expressions
    For more options, see :man grep

3, Commonly used grep example

(1) Multiple file queries
    grep "sort" *.doc       # See file name matching

(2) Line matching : Output the count of matching rows
    grep -c "48" data.doc   # The output document contains 48 The number of lines of the character

(3) Show matching rows and number of rows
    grep -n "48" data.doc       # Show all matches 48 Your line and line number

(4) Show unmatched lines
    grep -vn "48" data.doc      # The output does not contain 48 The line of

(4) Show unmatched lines
    grep -vn "48" data.doc      # The output does not contain 48 The line of

(5) Case sensitive
    grep -i "ab" data.doc       # The output contains ab or Ab The line of the string

4, Applications of regular expressions

(1) Applications of regular expressions ( Be careful : It's better to enclose regular expressions in single quotation marks )
    grep '[239].' data.doc      # Output all the data containing to 2,3 or 9 At the beginning , And it's a line of two numbers

(2) Mismatch test
    grep '^[^48]' data.doc      # The mismatch starts with 48 The line of

(3) Use extended pattern matching
    grep -E '219|216' data.doc

(4) ...
    This needs to be applied and summarized in practice , Master regular expression .

5, Use the class name
You can use international pattern matching class names :
[[:upper:]]   [A-Z]
[[:lower:]]   [a-z]
[[:digit:]]   [0-9]
[[:alnum:]]   [0-9a-zA-Z]
[[:space:]]    Space or tab
[[:alpha:]]   [a-zA-Z]

(1) Use
    grep '5[[:upper:]][[:upper:]]' data.doc     # Query to 5 A line that begins with two capital letters and ends with