grep

http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html

 

The grep command in Linux is a powerful text search tool that uses regular expressions to search for text and print out the matching lines. The full name of grep is Global Regular Expression Print, which means the global regular expression version, and its use rights are all users.

The way grep works is that it searches one or more files for a string template. If the template includes spaces, it must be quoted, and all strings following the template are treated as filenames. The results of the search are sent to standard output and do not affect the contents of the original file.

grep can be used in shell scripts because grep indicates the status of the search by returning a status value, 0 if the template search was successful, 1 if the search was unsuccessful, and 2 if the searched file does not exist. We can use these return values ​​to do some automated text processing.

 

1. Command format:

grep [option] pattern file

2. Command function:

Specific characters for filtering/searching. Regular expressions can be used in conjunction with various commands, which is very flexible in use.

3. Command parameters:

-a --text #Do not ignore binary data.   

-A<display line number> --after-context=<display line number> #In addition to displaying the column that conforms to the template style, and display the content after the line.   

-b --byte-offset #Indicate the number of the first character of the line before displaying the line that matches the style.   

-B<display line number> --before-context=<display line number> #In addition to displaying the line that conforms to the style, and display the content before the line.   

-c --count #Count the number of columns that match the style.   

-C<display line number> --context=<display line number>or-<display line number> #In addition to displaying the line that conforms to the style, and display the content before and after the line.   

-d <action> --directories=<action> #This parameter must be used when specifying that the directory to be searched is not a file, otherwise the grep command will report information and stop the action.   

-e<template style> --regexp=<template style> #Specify a string as the style for finding file content.   

-E --extended-regexp #Use the style as extended normal notation.   

-f<rule file> --file=<rule file> #Specify the rule file, its content contains one or more rule styles, let grep find the file content that meets the rule conditions, the format is one rule style per line.   

-F --fixed-regexp # Treat styles as a list of fixed strings.   

-G --basic-regexp # Treat styles as normal notation.   

-h --no-filename #Do not indicate the name of the file to which the line belongs before displaying the line that matches the style.   

-H --with-filename #Before displaying the line that matches the style, it indicates the name of the file to which the line belongs.   

-i --ignore-case #Ignore differences in character case.   

-l --file-with-matches #List file names whose content matches the specified style.   

-L --files-without-match #List file names whose contents do not conform to the specified style.   

-n --line-number #Indicate the column number of the line before displaying the line that matches the style.   

-q --quiet or --silent #Do not display any information.   

-r --recursive #The effect of this parameter is the same as specifying the "-d recurse" parameter.   

-s --no-messages #Do not display error messages.   

-v --revert-match #Display all lines that do not contain matching text.   

-V --version #Display version information.   

-w --word-regexp #Only display the columns that match the whole word.   

-x --line-regexp #Only display all columns that match.   

-y #The effect of this parameter is the same as specifying the "-i" parameter.

  

4. Regular expression:

Regular expression for grep:

^ #Anchor the start of a line For example: '^grep' matches all lines starting with grep.    

$ #Anchor the end of the line eg: 'grep$' matches all lines ending with grep.    

. # matches a non-newline character such as: 'gr.p' matches gr followed by any character, then p.    

* # matches zero or more preceding characters eg: '*grep' matches all lines with one or more spaces followed by grep.    

.* #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 in the specified range, such as: '[^A-FH-Z]rep' matches the beginning of a letter that does not contain AR and TZ, followed by the line of rep.    

\(..\) #Mark matching characters, such as '\(love\)', love is marked as 1.    

\< # Anchor the start of a word, eg: '\<grep' matches a line containing a word starting with grep.    

\> # Anchor the end of a word, eg 'grep\>' matches a line containing a word ending in grep.    

x\{m\} #Repeat the character x, m times, such as: '0\{5\}' matches the line containing 5 o.    

x\{m,\} #Repeat the character x, at least m times, such as: 'o\{5,\}' matches at least 5 lines of o.    

x\{m,n\} #Repeat the character x, at least m times, not more than n times, such as: 'o\{5,10\}' matches 5--10 lines of o.   

\w #Matches alphanumeric characters, i.e. [A-Za-z0-9], eg: 'G\w*p' matches G followed by zero or more alphanumeric characters, then p.   

\W # The inverse of \w, which matches one or more non-word characters, such as periods, periods, etc.   

\b #word lock character, such as: '\bgrep\b' only matches grep.  

 

POSIX characters:

In order to keep one to one in the character encoding of different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [:alnum:] is another way of writing [A-Za-z0-9]. Put them in [] to become regular expressions, such as [A-Za-z0-9] or [[:alnum:]]. Except for fgrep, grep under linux supports POSIX character classes.

 

[:alnum:] #Alphanumeric characters   

[:alpha:] #Text character   

[:digit:] #digital characters   

[:graph:] #Non-blank characters (non-space, control characters)   

[:lower:] #lowercase characters   

[:cntrl:] #Control characters   

[:print:] #Non-blank characters (including spaces)   

[:punct:] #Punctuation   

[:space:] #All whitespace characters (newlines, spaces, tabs)   

[:upper:] #Uppercase characters   

[:xdigit:] #hexadecimal digits (0-9, af, AF)  

5. Example of use:

Example 1: Find the specified process

Order:

ps -ef | grep svn

output:

[root @ localhost ~] # ps -ef | grep svn

root 4943   1      0  Dec05 ?   00:00:00 svnserve -d -r /opt/svndata/grape/

root 16867 16838  0 19:53 pts/0    00:00:00 grep svn

[root@localhost ~]#

illustrate:

The first record is the process found; the second result is the grep process itself, not the process you are really looking for.

 

Example 2: Find the number of specified processes

Order:

ps -ef | grep svn -c

ps -ef | grep -c svn

output:

[root @ localhost ~] # ps -ef | grep svn -c

2

[root @ localhost ~] # ps -ef | grep -c svn 

2

[root@localhost ~]#

illustrate:

 

Example 3: Read keywords from a file to search

Order:

cat test.txt | grep -f test2.txt

output:

[root@localhost test]# cat test.txt 

hnlinux

peida.cnblogs.com

ubuntu

ubuntu linux

redhat

Redhat

linuxmint

[root@localhost test]# cat test2.txt 

linux

Redhat

[root@localhost test]# cat test.txt | grep -f test2.txt

hnlinux

ubuntu linux

Redhat

linuxmint

[root@localhost test]#

illustrate:

Output the content lines in the test.txt file that contain the keywords read from the test2.txt file

 

Example 3: Read keywords from a file to search and display line numbers

Order:

cat test.txt | grep -nf test2.txt

output:

[root@localhost test]# cat test.txt 

hnlinux

peida.cnblogs.com

ubuntu

ubuntu linux

redhat

Redhat

linuxmint

[root@localhost test]# cat test2.txt 

linux

Redhat

[root@localhost test]# cat test.txt | grep -nf test2.txt

1:hnlinux

4:ubuntu linux

6:Redhat

7: linuxmint

[root@localhost test]#

 

illustrate:

Output the content lines in the test.txt file containing the keywords read from the test2.txt file, and display the line number of each line

 

Example 5: Find keywords from a file

Order:

grep 'linux' test.txt

output:

[root@localhost test]# grep 'linux' test.txt 

hnlinux

ubuntu linux

linuxmint

[root@localhost test]# grep -n 'linux' test.txt 

1:hnlinux

4:ubuntu linux

7: linuxmint

[root@localhost test]#

 

illustrate:

 

Example 6: Find keywords from multiple files

Order:

grep 'linux' test.txt test2.txt

output:

[root@localhost test]# grep -n 'linux' test.txt test2.txt 

test.txt:1:hnlinux

test.txt:4:ubuntu linux

test.txt: 7: linuxmint

test2.txt:1:linux

[root@localhost test]# grep 'linux' test.txt test2.txt 

test.txt:hnlinux

test.txt:ubuntu linux

test.txt: linuxmint

test2.txt:linux

[root@localhost test]#

 

illustrate:

When there are multiple files, when outputting the queried information content line, the name of the file will be output at the top of the line and ":" will be added as an identifier

 

Example 7: grep does not display its own process

Order:

ps aux|grep \[s]sh

ps aux | grep ssh | grep -v "grep"

output:

[root@localhost test]# ps aux|grep ssh

root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 

root  16901  0.0  0.0  61180   764 pts/0  S+   20:31   0:00 grep ssh

[root@localhost test]# ps aux|grep \[s]sh]

[root@localhost test]# ps aux|grep \[s]sh

root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 

[root@localhost test]# ps aux | grep ssh | grep -v "grep"

root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0

 

illustrate:

 

Example 8: Find the content of the line starting with u

Order:

cat test.txt |grep ^u

output:

[root@localhost test]# cat test.txt |grep ^u

ubuntu

ubuntu linux

[root@localhost test]#

illustrate:

 

Example 9: Output the line content that does not start with u

Order:

cat test.txt |grep ^[^u]

output:

[root@localhost test]# cat test.txt |grep ^[^u]

hnlinux

peida.cnblogs.com

redhat

Redhat

linuxmint

[root@localhost test]#

illustrate:

 

Example 10: Output line content ending with hat

Order:

cat test.txt |grep hat$

output:

[root@localhost test]# cat test.txt |grep hat$

redhat

Redhat

[root@localhost test]#

illustrate:

 

Example 11:

Order:

output:

[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"

          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0

[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"

          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0

[root@localhost test]#

 

illustrate:

 

Example 12: Displaying content lines containing ed or at characters

Order:

cat test.txt |grep -E "ed|at"

output:

[root@localhost test]# cat test.txt |grep -E "peida|com"

peida.cnblogs.com

[root@localhost test]# cat test.txt |grep -E "ed|at"

redhat

Redhat

[root@localhost test]#

illustrate:

 

Example 13: Display all lines in files ending in .txt under the current directory that contain strings with at least 7 consecutive lowercase characters per string

Order:

grep '[a-z]\{7\}' *.txt

output:

[root@localhost test]# grep '[a-z]\{7\}' *.txt

test.txt:hnlinux

test.txt:peida.cnblogs.com

test.txt: linuxmint

[root@localhost test]#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850006&siteId=291194637