Linux grep command, powerful text search __linux

Source: Internet
Author: User
Tags character classes control characters documentation grep regular expression posix apache log egrep
1. The role of the grep command in Linux is a powerful text search tool that can use regular expressions to search for text and print matching rows. The grep full name is global Regular Expression Print, which represents the global regular expression version, and its use permissions are all users. 2. Format grep [options] 3. Main parameters [options] Main parameters:-C: Only the count of matching rows is output. -I: case-insensitive (applies to given only). -H: Do not display file names when querying multiple files. -L: Only file names that contain matching characters are output when querying multiple files. -N: Displays matching rows and line numbers. -S: Do not display error messages that do not exist or do not match text. -V: Displays all rows that do not contain matching text. Pattern Regular Expression Main parameter:/: Ignores 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. /&lt: Starts with the line that matches the regular expression. /&gt: To the end of the line that matches the regular expression. []: A single character, such as [a], that is, a meets the requirements. [-]: range, such as [A-z], that is, a, B, C until Z all meet the requirements ... : all the individual characters. *: There are characters, the length can be 0. The 4.grep command uses a simple instance $ grep ' test ' d* to display all the rows that contain test in a file that starts with D. $ grep ' test ' AA bb cc shows the line matching test in the aa,bb,cc file. $ grep ' [a-z]/{5/} ' AA shows all lines that contain at least 5 consecutive lowercase characters for each string. $ Grep ' w/(es/) T.*/1′aa if West is matched, es is stored in memory and labeled as 1 and then searches for any character (. *), followed by another ES (/1), and the line is displayed. If you use Egrep or GREP-E, you do not use the "/" number to escape, directly written ' W (es) t.*/1′ on it. The 5.grep command uses complex instances to assume that you are searching for files with string ' magic ' in the '/usr/src/linux/doc ' directory: $ grep magic/usr/src/linux/doc/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* I Use the Magic sysrq key? Where the file ' Sysrp.txt ' contains the string, the SYSRQ function is discussed. By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' will be listed in the following form:
Grep:sound:Is A directory This may make ' grep ' output difficult to read. Here are two ways to solve this problem:
Explicitly require searching subdirectories: Grep-r
or Ignore subdirectories: grep-d Skip if you have a lot of output, you can go through the pipe to read it on ' less ':
$ grep magic/usr/src/linux/documentation/* | Less
This way, you can read it more easily. It is important to note that you must provide a file filtering method (*) for all files to be searched. If you forget, ' grep ' will wait until the program is interrupted. If you encounter such a situation, press <ctrl c>, and then try again. Here are some of the more interesting command line arguments:
Grep-i pattern Files: case-insensitive search. The default case is case-sensitive,
Grep-l pattern Files: Only the matching file names are listed,
Grep-l pattern Files: Lists mismatched file names,
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
Grep-c number pattern files: matching contexts show [number] rows,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2. Here are some special symbols for searching:
/< and/> Each mark the beginning and the end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ' and so on,
grep '/<man ' * matches ' manic ' and ' man ', but not ' Batman ',
grep '/<man/> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to the matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line, ===========================================================================

1. grep Command Introduction

grep (Global search Regular expression (RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching rows. The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is an extension of grep that supports more re metacharacters, Fgrep is fixed grep or fast grep, which regards all letters as words, that is, the metacharacters in the regular expression returns to its own literal meaning and is no longer special. Linux uses the GNU version of grep. It is more powerful and can use the EGREP and FGREP functions through the-G,-E,-F command-line Options.

grep works like this by searching 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 file names. The results of the search are sent to the screen without affecting the contents of the original file.

grep is available for shell scripts because grep indicates the state of the search by returning a status value, returns 0 if the template search succeeds, or 1 if the search is unsuccessful, and returns 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 beginning of the anchoring line, such as: ' ^grep ' matches all rows beginning with grep.

$
The end of the anchoring line, such as: ' grep$ ', matches all rows that end with grep.

.
Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P.

*
Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces followed by the grep row ... * 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 ' match does not contain a letter beginning with A-r and T-z, followed by the rep line.

/(.. /)
Tags match characters, such as '/(love/) ', and Love is marked as 1.

/<
Anchor the beginning of a word, as: '/

/>
Anchors the end of a word, such as ' grep/> ' to match 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 character X, at least m times, such as: ' o/{5,/} ' matches rows with at least 5 O.

x/{m,n/}
Repeat character X, at least m times, no more than n times, such as: ' o/{5,10/} ' matches 5–10 O's line.

/w
Matches literal and numeric characters, that is, [a-za-z0-9], such as: ' G/w*p ' matches with G followed by 0 or more text or number characters, followed by P.

/w
/w, which matches one or more non word characters, such as the dot number period.

/b
Word locks, such as: '/bgrep/b ' only match grep.

3. Meta-character extension set for Egrep and GREP-E

+
Matches one or more of the previous characters. such as: ' [a-z]+able ', match one or more lowercase letters followed by the able string, such as loveable,enable,disable.

?
Matches 0 or more previous characters. For example: ' Gr?p ' matches a gr followed by one or no characters, then a line of P.

A|b|c
Match A or B or C. such as: grep|sed match grep or SED

()
Group 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

To maintain consistency in the character encoding of different countries, POSIX (the Portable operating System Interface) adds special character classes, such as [: Alnum:] is another example of a-za-z0-9. To put them inside the [] number, you can become regular expressions, such as [a-za-z0-9] or [[: Alnum:]]. Under Linux, grep supports the POSIX character classes except Fgrep.

[: Alnum:]
Literal numeric character

[: Alpha:]
Literal characters

[:d Igit:]
numeric characters

[: Graph:]
Non-null characters (not spaces, control characters)

[: Lower:]
lowercase characters

[: Cntrl:]
Control characters

[:p rint:]
Non-null characters (including spaces)

[:p UNCT:]
Punctuation

[: Space:]
All white-space characters (new lines, spaces, tabs)

[: Upper:]
Uppercase characters

[: Xdigit:]
hexadecimal digits (0-9,a-f,a-f)

5. grep command Options

-?
Displays both the top and bottom of the matching row. Rows, such as: grep-2 pattern filename Displays the top and bottom 2 rows of matching rows at the same time.

-b,–byte-offset
Print the block number in which the line is printed before the matching line.

-c,–count
Prints only the number of rows that match and does not display the matching content.

-F File,–file=file
Extracts the template from the file. The empty file contains 0 templates, so nothing matches.

-h,–no-filename
When searching for multiple files, the matching filename prefix is not displayed.

-i,–ignore-case
ignores case differences.

-q,–quiet
Suppresses display and returns only the exit status. 0 indicates that a matching row was found.

-l,–files-with-matches
Print a list of files that match the template.

-l,–files-without-match
Print a list of files that do not match the template.

-n,–line-number
Print the line number before the matching line.

-s,–silent
Does not display error messages about the absence or inability to read files.

-v,–revert-match
Reverse retrieve, showing only rows that do not match.

-w,–word-regexp
If referenced by/< and/>, the expression is searched as a word.

-v,–version
Displays software version information.

6. Examples

grep Huanxiangwu huanxiangwu.txt Display lines of text containing Huanxiangwu

grep 404/var/log/httpd/access_log search Access_log file for 404

PS Auwx | grep init Displays the PS command output line containing init

PS Auwx | grep "/[*/]" displays the commands that are enclosed in brackets in the PS command output to find out if PS cannot display its options

DMESG | grep "[]ata/|^ata" checks the kernel ring output list to find ATA device information such as hard disk and optical drive

Grep-r virtualhost/etc/httpd/conf* Recursive lookup string VirtualHost

Grep-rn virtualhost/etc/httpd/conf* recursively finds the string virtualhost and finds the search target specific

Grep–color-rn virtualhost/etc/httpd/conf* recursively finds the string virtualhost and finds the specific row of the search target and displays it as color

Grep-h sshd/var/log/secure* does not display file name

Grep-i Selinux/var/log/messages Search SELinux case-insensitive

Grep-rl virtualhost/etc/httpd/conf* displays only the file name that contains the retrieved string

Grep-v "200″/var/log/httpd/access_log* Search Access_log file does not display all rows that do not match the search string

grep 192.168.56.1/var/log/httpd/access_log | Wc-l searches Apache log files and counts the number of visits from 192.168.56.1ip

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

To use a good grep this tool, in fact, is to write a good regular expression, so here is not the grep all the features of the example 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 pipe, showing only the rows that start with a.

$ grep ' test ' d*

Displays the rows that contain test in all files that start with D.

$ grep ' test ' AA bb cc

Displays the row that matches test in the aa,bb,cc file.

$ grep ' [a-z]/{5/} ' AA

Displays all the lines that contain at least 5 consecutive lowercase characters for each string.

$ Grep ' w/(es/) T.*/1 ' AA

If West is matched, the es are stored in memory and labeled 1, then search for any character (. *), followed by another ES (/1), and the line is displayed. If you use Egrep or GREP-E, you do not use the "/" number to escape, directly written ' W (es) T.*/1 ' on it. (T002)

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

Search for a text file with ' grep '
If you want to find a string in several text files, you can use the ' grep ' command. ' grep ' searches the text for the specified string.
Suppose you are searching for a file with a string ' magic ' in the '/usr/src/linux/documentation ' directory:

$ grep magic/usr/src/linux/documentation/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* I Use the Magic sysrq key?

Where the file ' Sysrp.txt ' contains the string, the SYSRQ function is discussed.

By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' will be listed in the following form:

Grep:sound:Is a Directory

This may make the output of ' grep ' difficult to read. Here are two ways to solve this problem:

Explicitly require searching subdirectories: Grep-r
or ignores subdirectories: grep-d Skip
Of course, if you anticipate a lot of output, you can go through the pipe and transfer it to ' less ' for reading:

$ grep magic/usr/src/linux/documentation/* | Less

This way, you can read it more easily.

It is important to note that you must provide a file filtering method (*) for all files to be searched. If you forget, ' grep ' will wait until the program is interrupted. If you encounter such a situation, press <ctrl c>, and then try again.

Here are some interesting command line arguments:

Grep-i pattern Files: case-insensitive search. The default case is case-sensitive,
Grep-l pattern Files: Only the matching file names are listed,
Grep-l pattern Files: Lists mismatched file names,
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
Grep-c number pattern files: matching contexts show [number] rows,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.
Here are some special symbols for searching:

/< and/> Each mark the beginning and the end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ' and so on,
grep '/<man ' * matches ' manic ' and ' man ', but not ' Batman ',
grep '/<man/> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to the matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line,
If you are not accustomed to command-line arguments, try the ' grep ' of the graphical interface, such as Rexgrep. The software provides syntax such as and, or, not, and beautiful button:-). If you just need a clearer output, try Fungrep.


Article reprinted from the Home network: http://www.bitscn.com/os/linux/200701/97557.html

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.