Detailed explanation of linux commands-grep commands (text search tool)

Source: Internet
Author: User
Tags character classes
In Linux, the grep command is a powerful text search tool that uses regular expressions to search for text and print matching lines.

In Linux, the grep command is a powerful text search tool that uses regular expressions to search for text and print matching lines. Grep stands for Global Regular Expression Print, which indicates the Global Regular Expression version. its permission is granted to all users.
Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced. all strings after the template are treated as file names. The search result is sent to the standard output without affecting the content of the original file.
Grep can be used in shell scripts because grep returns a status value to indicate the search status. if the template search is successful, 0 is returned. if the search is unsuccessful, 1 is returned, if the searched file does not exist, 2 is returned. We can use these return values to automate text processing.

1. command format:

Copy codeThe code is as follows:
Grep [option] pattern file

2. command functions:
Specific characters used for filtering/searching. Regular expressions can be used in combination with multiple commands.

3. command parameters:
-A -- text # Do not ignore binary data.
- <显示行数> -- After-context = <显示行数> # In addition to displaying the column that conforms to the template style, the content after the row is displayed.
-B -- byte-offset # indicates the number of the first character of the line before the line that conforms to the style is displayed.
-B <显示行数> -- Before-context = <显示行数> # In addition to displaying the line that meets the style, and displaying the content before the line.
-C -- count # calculate the number of columns that match the style.
-C <显示行数> -- Context = <显示行数> Or- <显示行数> # In addition to displaying the line that meets the style, and displaying the content before and after the line.
-D <动作> -- Directories = <动作> # This parameter must be used when you specify a directory rather than a file to be searched. Otherwise, the grep command returns information and stops the operation.
-E <范本样式> -- Regexp = <范本样式> # Specify a string as the style for searching the file content.
-E -- extended-regexp # use the style as an extended normal notation.
-F <规则文件> -- File = <规则文件> # Specify the rule file. the content of the rule file contains one or more rule styles. let grep search for the file content that meets the rule conditions. The format is one rule style per line.
-F -- fixed-regexp # lists the styles as fixed strings.
-G -- basic-regexp # Use styles as normal notation.
-H -- no-filename # The name of the file to which the row conforms to the style is not displayed.
-H -- with-filename # indicates the name of the file to which the row belongs before the row that conforms to the style is displayed.
-I -- ignore-case # ignore case sensitivity differences.
-L -- file-with-matches # lists the names of objects whose contents match the specified style.
-L -- files-without-match # lists the names of files whose contents do not conform to the specified style.
-N -- line-number # indicates the number of columns of the row before the row that conforms to the style is displayed.
-Q -- quiet or -- silent # No information is displayed.
-R -- recursive # The effect of this parameter is the same as that of the specified "-d recurse" parameter.
-S -- no-messages # The error message is not displayed.
-V -- revert-match # displays all rows that do not contain matched text.
-V -- version # displays the version information.
-W -- word-regexp # Only columns with full-text matching are displayed.
-X -- line-regexp # only displays columns that match the full column.
-Y # The effect of this parameter is the same as that of the specified "-I" parameter.

4. rule expression:
Grep rule expression:
^ # Start of the anchor row: '^ grep' matches all rows starting with grep.
$ # End of the anchor row, for example, 'grep $ 'matches all rows ending with grep.
. # Match a non-linefeed character, for example, 'Gr. P' matches gr followed by any character, followed by p.
* # Match zero or multiple previous characters, for example, '* grep' matches all rows followed by one or more spaces.
. * # Represents any character together.
[] # Match a character in a specified range, for example, '[Gg] rep' matches Grep and grep.
[^] # Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by rep.
\ (.. \) # Mark matching characters, such as '\ (love \)', and love is marked as 1.
\ <# Specify the start of a word, for example :'\ \># Specify the end of a word, such as 'grep \>.
X \ {m \} # Repeated characters x, m times, for example, '0 \ {5 \} 'match rows containing 5 o.
X \ {m, \} # Repeated characters x, at least m times, such as: 'O \ {5, \} 'matching rows with at least 5 o.
X \ {m, n \} # Repeated character x, at least m times, no more than n times, for example, 'O \ {5, 10 \} 'matches rows of 5-10 o.
\ W # match text and numeric characters, that is, [A-Za-z0-9], such as: 'G \ w * p' match with G followed by zero or more characters or numbers, then p.
The inverse form of \ W # \ w. it matches one or more non-word characters, such as periods and periods.
\ B # The word lock. for example, '\ bgrep \ B' matches only grep.
POSIX characters:
To ensure one character encoding in 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 the [] sign to become A regular expression, such as [A-Za-z0-9] or [[: alnum:]. In linux, grep supports POSIX character classes except fgrep.
[: Alnum:] # numbers and characters
[: Alpha:] # text characters
[: Digit:] # numeric characters
[: Graph:] # Non-empty characters (non-space, control characters)
[: Lower:] # Lowercase characters
[: Cntrl:] # control characters
[: Print:] # Non-empty characters (including spaces)
[: Punct:] # Punctuation marks
[: Space:] # All blank characters (new lines, spaces, and tabs)
[: Upper:] # uppercase characters
[: Xdigit:] # hexadecimal number (0-9, a-f, A-F)

5. use instance:
Instance 1: Find the specified process
Command: ps-ef | grep svn
Output:

Copy codeThe code is as follows:
[Root @ localhost ~] # Ps-ef | grep svn
Root 4943 1 0 Dec05? 00:00:00 svnserve-d-r/opt/svndata/grape/
Root 16867 16838 0 00:00:00 pts/0 grep svn
[Root @ localhost ~] #

Note: The first record is the process to be searched; the second record is the grep process itself, not the process to be searched.

Instance 2: Find the number of specified processes
Command:

Copy codeThe code is as follows:
Ps-ef | grep svn-c
Ps-ef | grep-c svn

Output:

Copy codeThe code is as follows:
[Root @ localhost ~] # Ps-ef | grep svn-c
2
[Root @ localhost ~] # Ps-ef | grep-c svn
2
[Root @ localhost ~] #

Instance 3: read keywords from files for search
Command: cat test.txt | grep-f test2.txt
Output:

Copy codeThe code is as follows:
[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] #

Note:
The output file test.txt contains the content line containing the keywords read from the file test2.txt.

Instance 3: read keywords from files for search and display row numbers
Command: cat test.txt | grep-nf test2.txt
Output:

Copy codeThe code is as follows:
[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] #

Note:
The output test.txt file contains the content line of the keywords read from the test2.txt file, and displays the row number of each row.

Instance 5: search for keywords from files
Command: grep 'Linux 'test.txt
Output:

Copy codeThe code is as follows:
[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] #

Instance 6: search for keywords from multiple files
Command: grep 'Linux 'test.txt test2.txt
Output:

Copy codeThe code is as follows:
[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] #

Note: When multiple files are output, the file name is output at the beginning of the row and ":" is added as the identifier.

Instance 7: grep does not display its own process
Command:

Copy codeThe code is as follows:
Ps aux | grep \ [s] sh
Ps aux | grep ssh | grep-v "grep"

Output:

Copy codeThe code is as follows:
[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 sshd: root @ pts/0
Root 16901 0.0 0.0 61180 764 pts/0 S + 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 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 sshd: root @ pts/0

Instance 8: find the row content starting with u
Command: cat test.txt | grep ^ u
Output:

Copy codeThe code is as follows:
[Root @ localhost test] # cat test.txt | grep ^ u
Ubuntu
Ubuntu linux
[Root @ localhost test] #

Instance 9: output the content of rows not starting with u
Command: cat test.txt | grep ^ [^ u]
Output:

Copy codeThe code is as follows:
[Root @ localhost test] # cat test.txt | grep ^ [^ u]
Hnlinux
Peida.cnblogs.com
Redhat
Redhat
Linuxmint
[Root @ localhost test] #

Instance 10: output the row content ending with hat
Command: cat test.txt | grep hat $
Output:

Copy codeThe code is as follows:
[Root @ localhost test] # cat test.txt | grep hat $
Redhat
Redhat
[Root @ localhost test] #

Instance 11:
Command: ifconfig eth0 | grep "[0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}"
Output:

Copy codeThe code is as follows:
[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] #

Instance 12: displays the row containing the ed or at characters.
Command: cat test.txt | grep-E "ed |"
Output:

Copy codeThe code is as follows:
[Root @ localhost test] # cat test.txt | grep-E "peida | com"
Peida.cnblogs.com
[Root @ localhost test] # cat test.txt | grep-E "ed |"
Redhat
Redhat
[Root @ localhost test] #

Instance 13: displays all rows in the file ending with .txt in the current directory that contain strings with at least seven consecutive lowercase characters.
Command: grep '[a-z] \ {7 \}' *. txt
Output:

Copy codeThe code is as follows:
[Root @ localhost test] # grep '[a-z] \ {7 \}' *. txt
Test.txt: hnlinux
Test.txt: peida.cnblogs.com
Test.txt: linuxmint
[Root @ localhost test] #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.