Linux commands (1)-grep

Source: Internet
Author: User
Tags character classes control characters grep regular expression posix egrep

1. grep

function : Find the string that matches the condition in the file.

syntax :grep [-abcefghhillnqrsvvwxy][-a< show Columns >][-B< show columns >][-C< Show Columns >][-d < action >][-e< template style >][-f< template file >][--help][template style [file or directory ...]

Supplemental Note : Thegrep directive is used to find the file containing the specified template style, and if the contents of a file are found to conform to the specified template style, the preset grep directive will display the column containing the template style. If no file name is specified, or if the given file name is "-", the grep instruction reads the data from the standard input device.

Parameters:

-A or--text to find binary files in text file mode

-a< Displays the number of columns > or--after-context=< Displays the number of columns > displays the contents after the column, except for the column that conforms to the template style.

-B or--byte-offset indicates the bit number of the first character of the column before displaying the column that conforms to the template style.

-b< Displays the number of columns > or--before-context=< Displays the number of columns > In addition to displaying the column that conforms to the template style, and displays the contents before the column .

-C or--count calculates the number of columns that conform to the template style.

--color=auto find keywords plus color.

-c< Displays the number of columns > or--context=< Displays the number of columns > or-< Displays the number of columns > In addition to displaying the column that conforms to the template style, and displays the contents before and after the column .

-d< action > or--directories=< action > You must use this parameter when you specify that you want to find a directory rather than a file, otherwise the GREP directive returns information and stops the action.

-e< template style > or--regexp=< template style > Specify string as the template style for finding the contents of a file.

-E or--extended-regexp uses the template style as an extended normal notation.

-f< template File > or--The file =< template file > Specify a template file with one or more template styles, so grep finds the content of the files that match the template criteria, in the form of a template style for each column.

-F or--fixed-regexp treats the template style as a list of fixed strings.

-G or--basic-regexp use the template style as normal notation.

-H or--no-filename does not indicate the file name that the column belongs to until it displays the column that conforms to the template style.

-H or--with-filename indicates the file name that the column belongs to before it displays the column that conforms to the template style.

-I or--ignore-case ignores the difference in the case of characters.

-L or--file-with-matches lists file names that match the file contents to the specified template style.

-L or--files-without-match lists file names that do not conform to the specified template style.

- N or--line-number indicates the column number of the column before displaying the column that conforms to the template style .

-Q or--quiet or--silent does not display any information.

-R or--recursive the effect of this parameter is the same as specifying the "-D recurse" parameter.

-S or--no-messages does not display an error message.

- V or--revert-match reverse lookup .

-V or--version displays version information.

-W or--WORD-REGEXP displays only the columns that match the whole word.

-X or--LINE-REGEXP displays only columns that are eligible for all columns.

-Y the effect of this parameter is the same as specifying the "-i" parameter.

--help online Help.

2, the use of grep:
Grep-r: explicitly require search subdirectories

grep-d Skip: or Ignore subdirectories

Grep-i pattern Files: Search by case-insensitive. The default case is case-sensitive,
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.

Use of grep and special symbols:

grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep \ ' <man\ ' * matches ' manic ' and ' man ', but not ' Batman ',
grep \ ' <man>\ ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
\ ' ^\ ': refers to a matching string at the beginning of the line,
\ ' $\ ': refers to a matching string at the end of the line,

grep search string

grep string filename

^m a line starting with M, ^ denotes the beginning meaning
m$ the line ending with M, $ denotes the end meaning
^[0-9] Lines starting with a number, [] can be listed in letters
^[124AB] Lines beginning with 1,2,4,a, or b
^b.503 period denotes any letter
* Asterisks indicate more than 0 letters (not available)
+ PLUS sign means more than 1 letters
. Slash can remove special meaning

eg

Cat passwd | grep ^b Lists the list of applicants for the university department
Cat passwd | grep ^s List of exchange student application account holders
Cat passwd | grep \ ' ^b.503\ ' lists the various grades of the motor system ...
grep \ ' ^.\ ' myfile.txt lists all lines beginning with a period

3. grep regular expression meta-character set

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

The end of the anchor line, such as: \ ' grep$\ ' matches all lines that end 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 grep line. * Together with any character represented.

[] 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-r and t-z, immediately following the line of the Rep.

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

< anchoring the beginning of a word, such as: \ '

> Anchors the end of a word, such as \ ' grep>\ ' matches a line containing a word that ends with grep.

X{m} repeats characters x,m times, such as: \ ' 0{5}\ ' matches rows containing 5 O.

X{m,} repeats a character x, at least m times, such as: \ ' o{5,}\ ' matches at least 5 rows of O.

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

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

The inverted form of W w matches one or more non-word characters, such as the dot period.

The B-word lock, such as: \ ' bgrepb\ ' only matches grep.

Meta-character expansion set for Egrep and GREP-E

+ matches one or more previous characters. such as: \ ' [a-z]+able\ ', matching one or more lowercase letters followed by able string, such as loveable,enable,disable, etc.

? Matches 0 or more previous characters. such as: \ ' gr?p\ ' matches the GR followed by one or no characters, then the lines of P.

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

() grouping 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}

POSIX character class

In order to maintain the character encoding in different countries, POSIX (the portable Operating System Interface) adds special character classes, such as [: alnum:] a-za-z0-9 another notation. You can place them in the [] number to be a regular expression, such as [a-za-z0-9] or [[: Alnum:]]. grep under Linux supports POSIX character classes in addition to Fgrep.

[: Alnum:] literal numeric character

[: Alpha:] literal character

[:d igit:] numeric characters

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

[: Lower:] lowercase characters

[: Cntrl:] control character

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

[:p uNCT:] Punctuation

[: space:] All whitespace characters (new lines, spaces, tabs)

[: Upper:] Uppercase characters

[: xdigit:] hex digit (0-9,A-F,A-F)

Example

To use a good grep this tool, in fact, is to write a regular expression, so here does not have all the functions of grep 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 pipeline, displaying only the lines that begin with a.

$ grep \ ' test\ ' d*

Displays all rows that contain test in a file that begins with D.

$ grep \ ' test\ ' AA bb cc

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

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

Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.

$ grep \ ' W (es) t.*1\ ' AA

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

Linux commands (1)-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.