Linux Common Commands--grep

Source: Internet
Author: User

grep 文本过滤工具

Syntax format: grep "Options" "Pattern" "File" grep [parameter] [match pattern] [find file]
Attention:
1.grep is one of the most important commands in a Linux system, and its function is to filter matching rows and data from a text file or pipeline data stream.
The matching pattern or pattern match in the 2.grep command is something you want to find, which can be a normal text symbol or a regular expression.
Parameter options:

Parameters Description
-C Calculates the number of rows found ' search string '
-O Index out of matching content
-I. Case insensitive
-N Displays the line number of the matching content
-R You must use this parameter when you specify that you want to find a directory rather than a file, otherwise the grep command returns information and stops the action
-V Reverse selection, which is a row without a ' search string ' content
-L Lists file names for file contents that conform to the specified template style
-E Extended grep, or egrep, can be used to extend regular expressions
--color=auto Search keywords Display color
-V Display software version information

Case FILE:

Case one:
Examples of parameters



Note: You test some of the searches that contain regular expressions and find that EGREP can be implemented, and grep can be implemented as well. However, for example: Egrep-v "^$|#" file1.txt, grep will not be implemented because the EGREP adds the effect of the additional regular expression meta-character set, and for standard grep, when the extension metacharacters are preceded by \, grep will automatically enable the extended option-E

Case TWO:
grep regular Expression meta-character set

Character Set Description Case
\^ Start of anchoring lines such as: ' ^grep ' matches all lines that begin with grep
$ End of Anchor Line For example: ' grep$ ' matches all rows ending with grep
. Match a character that is not a line break such as: ' GR.P ' matches gr followed by an arbitrary character, then P
* Match 0 or more of previous characters such as: ' *grep ' matches all one or more spaces followed by the grep line. * Together with any character
[ ] Matches a character in a specified range such as: ' [gg]rep ' matches grep and grep
[^ ] Matches a character that is not within the specified range such as: ' [^a-fh-z]rep ' matches the beginning of a letter that does not contain a-f and h-z, immediately following the line of the rep
\(.. \) Tag matching characters such as: ' (Love) ', Love is marked as 1
\< Anchoring the beginning of a word For example: ' \<grep ' matches a line containing a word that begins with grep
\> Anchoring the end of a word For example: ' grep\> ' matches lines that contain words ending with grep
X{M} Consecutive repeating characters x,m times For example: ' O{5} ' matches rows containing 5 consecutive O
X{m,} Continuous repetition of character X, at least m times For example: ' O{5,} ' matches rows with at least 5 consecutive O
X{m,n} Repeat character X, at least m times, no more than n times such as: ' o{5,10} ' matches rows of 5-10 consecutive O
\w Matches a literal and numeric character that is also [a-za-z0-9] such as: ' G\w*p ' matches a G followed by 0 or more literal or numeric characters, then P
\w The inverted form of W, matching a non-word character such as: Dot Period, and so on. \w* can match multiple
\b Word lock character such as: ' \bgrep\b ' only match grep, that is, grep is only the word, both sides are spaces

Case THREE:
Common usage



Note: Empty lines and comment lines are removed
Case FOUR:
Search files recursively


Case FIVE:
Include or exclude specified files in grep search results
1.--exclude all files after the equals sign is excluded from the search results

2.--exclude-from exclude files from the FileList file list in search results

3.--include only recursively searches for the characters "and" in the specified directory file

Case SIX:
grep and Xargs using a 0-value byte suffix

Note: the-Z parameter is personally understood to suppress the line break "show content as a line", xargs-0 is to solve the problem of not "performing the display in one line" argument,-Z is usually used in conjunction with-L. (-z option to specify a value of 0 bytes as the Terminator file name (), xargs-0 reads the input and separates the file name with a value of 0 bytes Terminator)
Case Seven:
Print a line before or after matching text


Note: If there are multiple matching results, "--" will be used as a delimiter between each matching result, and the "--" delimiter will not be displayed if multiple results are connected or overlapping rows

Linux Common Commands--grep

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.