Linux under grep command

Source: Internet
Author: User

2. grep command

grep (Global search Regular expression (RE) and print out of the line, full search of 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 lines.

2.1. General options and examples of GREP commands

grep [OPTIONS] PATTERN [FILE ...]
grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]

The grep command searches for the pattern specified by the pattern parameter and writes each matching row to the standard output. These patterns are qualified regular expressions that use the ED or egrep command styles. If more than one name is specified in the file parameter, the grep command displays the name of the file that contains the matching row. Characters with special meaning to the shell ($, *, [, |, ^, (,), \) must be enclosed in double quotation marks when appearing in the pattern parameter. If the pattern parameter is not a simple string, you must usually enclose the entire pattern in single quotation marks. In expressions such as [A-z],-(minus) CML can specify a range based on the sequence that is currently being collated. A collation sequence can define an equivalent class for use in a character range. If no file is specified, grep is assumed to be standard input.

2.2. grep regular expression meta-character set (Basic set)

^ The start of the anchor line, such as: ' ^grep ' matches all lines that begin with grep.

The end of the anchor line is as follows: ' grep$ ' matches all rows ending with grep.

. Match a non-newline character such as: ' GR.P ' matches gr followed by an arbitrary character followed by P.

* Match 0 or more previous characters such as: ' *grep ' matches all one or more spaces followed by the line of 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 within the specified range, such as: ' [^a-fh-z]rep ' matches 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.

\< anchors the beginning of the word, such as: ' \<grep ' matches the line that contains the word that begins with grep.

\> anchors the end of the word, such as ' grep\> ' matches the line that contains the word that ends with grep.

X\{m\} repeats characters x,m times, such as: ' O\{5\} ' matches rows that contain 5 consecutive O.

X\{m,\} repeats the character x consecutively, at least m times, such as: ' O\{5,\} ' matches at least 5 consecutive O rows.

X\{m,n\} repeats the character x consecutively, at least m times, not more than n times, such as: ' O\{5,10\} ' matches rows that have a continuous 5--10 o.

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

\w W reverse form, matching a non-word character, such as the dot period and so on. \w* can match multiple.

\b Word lock, such as: ' \bgrep\b ' only matches grep, that is, grep is only the word, both sides are spaces.

2.3. Common options and examples of GREP commands

-?

Show matching rows up and down at the same time? Line, such as: grep-2 pattern filename Displays the top and bottom 2 rows of a matching row.

-b,--byte-offset

Prints the block number where the line is printed before the matching line.

-C,--Count

Only the number of matched rows is printed and the matching content is not displayed.

-F File,--file=file

Extract the template from the file. An empty file contains 0 templates, so nothing matches.

-h,--no-filename

When searching multiple files, the matching file name prefix is not displayed.

-i,--ignore-case

ignores case differences.

-q,--quiet

Cancels the display, returning only the exit status. 0 indicates that a matching row was found.

-l,--files-with-matches

Prints a list of files that match the template.

-l,--files-without-match

Prints a list of files that do not match the template.

-n,--line-number

Prints the line number in front of the matching line.

-s,--silent

does not display an error message about a file that does not exist or cannot be read.

-v,--revert-match

Anti-retrieval, displaying only rows that do not match.

-w,--word-regexp

If referenced by \< and \>, the expression is searched as a single word.

-v,--version

Displays software version information.

=====

Ls-l | grep ' ^a ' filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with a.

grep ' test ' d* shows all lines that contain test in a file that begins with D.

grep ' Test ' AA bb cc shows the line matching test in the aa,bb,cc file.

grep ' [A-z] ' AA displays all lines containing a string of at least 5 consecutive lowercase characters per string.

grep ' W (es) t.* ' AA if West is matched, then es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (), which is found to display the line. If you use Egrep or GREP-E, do not use the "" number to escape, directly written as ' W (es) t.* ' on it.

Grep-i pattern Files: Search by case-insensitive. Case sensitive by default

Grep-l pattern Files: Lists only the matching file names,

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 display [number] lines, respectively,

grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.

grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

Transferred from: http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html

Linux under grep command

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.