Number of grep commands and usage

Source: Internet
Author: User
Tags character classes posix egrep

Function Description: Search for matching strings in the file.

 

Syntax:Grep [-Abcefghhillnqrsvvwxy] [-A <display columns>] [-B <display columns>] [-C <display columns>] [-D <action>] [-E <template style>] [-F <Template File>] [-- help] [template style] [file or folder...]

Note:GrepCommand is used to search for files whose content includes the specified template style. If the content of a file conforms to the specified template style, the default grep command displays the column containing the template style. If no name is specified, or the given name is "-", the grep command reads data from the standard input device.

 

Number of shards:
-A or -- tExT do not ignore binary data.
-A <show the number of columns> or -- After-context = <show the number of columns> not only displays the column that conforms to the template style, but also displays the content after the column.
-B or -- byte-offSetThe first character Number of the column before displaying the column that conforms to the template style.
-B <display Number of columns> or -- before-context = <display Number of columns> In addition to displaying the column that conforms to the template style, and displaying the content before the column.
-C or -- count is used to calculate the number of columns that conform to the template style.
-C <display columns> or -- Context = <display columns> or-<display columns> displays the content before and after a column that conforms to the template style.
-D <perform action> or -- directories = <perform action> when you specify a folder rather than a file to be searched, you must use this number of partitions, otherwise, the grep command returns the information and stops the action.
-E <template style> or -- Regexp = <template style> specifies the string as the template style for searching the file content.
-E or -- extended-Regexp use the template style as an extended normal notation.
-F <Template File> or --File= <Template File> specifies a template file. Its content contains one or more template styles, allowing grep to search for the content of the file that meets the template conditions. The format is one template style for each column.
-F or -- fixed-Regexp treats the template style as a list of fixed strings.
-G or -- Basic-Regexp treats the template style as a normal notation.
-H or -- no-filename does not indicate the file name of the column before displaying the column that conforms to the template style.
-H or -- With-filename indicates the file name of the column before displaying the column that conforms to the template style.
-I or -- ignore-case ignores the differences between uppercase and lowercase characters.
-L or -- file-with-matches lists the file names that match the specified template style.
-L or -- files-without-match lists file names that do not match the specified template style.
-N or -- line-number indicates the number of columns in 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 the number of records is the same as that of the specified "-D recurse" records.
-S or -- no-messages Do Not Display error messages.
-V or -- revert-match reverse lookup.
-V or -- version displays the version number.
-W or -- word-Regexp only displays columns with full-text matching.
-X or -- 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.
-- Help Online Help.

Linux grepCommandUse
Use the grep command to search for text files from www.linuxso.com

Suppose you want to search for a string in several text files, you can use &LsQuo; grep 'command. 'Grep' searches for the specified string in the text.
If you are searching for a file with a string 'Magic 'in the'/usr/src/Linux/documentation' Folder:
$ Grep magic/usr/src/Linux/documentation /*
Sysrq.txt: * how do IEnableThe magic sysrq key?
Sysrq.txt: * how do I use the magic sysrq key?
 

When the file 'sysrp.txt 'contains this string, the function of sysrq is discussed.
By default, 'grep' only searches for the current folder. If there are many subfolders in this folder, 'grep' will be listed as follows:
Grep: Sound: is a directory
This may make the output of 'grep' difficult to read. There are two solutions:
Search for subfolders: grep-R
Or ignore the subfolders: grep-D skip
Of course, if you expect a lot of output, you can transfer it'Less'Read on
$ Grep magic/usr/src/Linux/documentation/* | less
In this way, you can read more conveniently.
Note that you must provide a file filtering method (* for searching all files *). Suppose you forget, 'grep' will wait until the program is interrupted. If this happens, press <Ctrl C> and try again.
The following are some interesting command line counters:
Grep-I pattern files: searches in uppercase and lowercase are not distinguished. By default, uppercase and lowercase letters are differentiated,
Grep-l pattern files: only lists the names of matched files,
Grep-l pattern files: Lists unmatched file names,
Grep-W pattern files: match only the entire word, not a part of the string (for example, match 'Magic ', not 'magical '),
Grep-C number pattern files: the matching context displays the rows of [number,
Grep pattern1 | pattern2 files: displays the rows matching pattern1 or pattern2,
Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2.
Here are some special symbols used for search:
<And> respectively mark the start and end of a word.
For example:
Grep man * matches 'Batman ', 'manic', 'Man ', etc,
Grep \ '<man \' * matches 'manic 'and 'man', but not 'Batman ',
Grep \ '<man> \' only matches 'man ', not other strings such as 'Batman' or 'manic.
\ '^ \': Indicates the first row of the matched string,
\ '$ \': Indicates the end of a matched string,
If you are not familiar with the number of arguments in the command line, you can try 'grep' in the graphic interface, such as rexgrep. This software provides and, or, not, and other syntaxes, as well as beautiful button :-). If you only need more explicit output, you 'd better try fungrep.

. Grep search string
Command Format:
Grep sTrIng filename
There are many ways to search for strings. For example, if you want to find all rows starting with M, you must introduce the pattern view.
Here are some simple examples and descriptions:
^ M indicates the start of a row starting with M. ^ indicates the start of a row.
M $ indicates the row ending with m, and $ indicates the end.
^ [0-9] a row starting with a number. Letters can be listed in [].
^ [124ab] rows starting with 1, 2, 4, A, or B
^ B .503 indicates any letter
* Asterisks indicate more than 0 letters (can be absent)
+ The plus sign indicates more than one letter.
. Diagonal lines can remove special meanings
<Eg>Cat Passwd| Grep ^ B list of applicants for the university account
Cat passwd | grep ^ s lists the list of Exchange Student Application accounts
Cat passwd | grep \ '^ B .503 \' list motor grades...
Grep \ '^. \' myfile.txt lists all rows starting with a period

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~

1. grep Introduction

 

Grep (Global Search regularExprEssion (re) and print out the line, comprehensive searchRegular ExpressionIs a powerful text search tool that can useRegular ExpressionSearch for text and print the matched rows. The UNIX grep family includes grep,EgrepAndFgrep. The commands of egrep and fgrep are only very different from those of grep. Egrep is an extension of grep and supports many other re metacharacters. fgrep is fixed grep or fast grep. They regard all the letters as words, that is, the metacharacters in the Regular Expression of the positive table indicate their literal meaning, and are no longer special. Linux uses the GNU version grep. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.

Grep works in this way. 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 considered as the file name. The search result is sent to the screen without affecting the content of the original file.

Grep can be used in shell scripts. 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 perform some self-active text processing work.
 

2. grep positive Table Partial-type metacharacter set (basic set)

 

^

The starting line of the anchor is as follows: \ '^ grep \' matches all rows starting with grep.

$

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

Match a non-linefeed character, for example, \ 'gr. p \ '. Match GR with a random character, followed by P.

*

Match zero or multiple previous characters such as: \ '* grep \' match all one or more spaces followed by grep rows. . * Represents random characters.

[]

Match a character in the specified range, for example, \ '[Gg] rep \' matches grep and grep.

[^]

Match a character that is not within the specified range, such as: \ '[^ A-FH-Z] rep \' match a letter that does not start with the A-R and T-Z, followed by the rep line.

(..)

Mark matching characters, such as \ '(love) \'. Love is marked as 1.

<

Anchor specifies the start of a word, for example :\'

>

Anchor specifies the end of a word. For example, \ 'grep> \ 'matches the row of a word ending with grep.

X {m}

Repeated characters X and M, for example, \ '0 {5} \ 'match rows containing 5 o.

X {M ,}

Repeated characters X, at least MB, for example, \ 'o {5,} \ 'match rows with at least 5 o.

X {m, n}

Repeated characters 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 number characters, that is, [A-Za-z0-9], such as: \ 'gw * p \ 'match with g followed by zero or multiple characters or numbers, followed by P.

W

Matches one or more non-word characters, such as periods, and periods.

 

B

The word lock character. For example, \ 'bgrepb \ 'only matches grep.

3. Meta character extension set for egrep and grep-e

+

Matches one or more previous characters. For example, \ '[A-Z] + able \' matches one or more lower-case letters with the able string, such as loveable, enable, and disable.

?

Matches zero or multiple previous characters. For example: \ 'gr? P \ 'matches gr followed by one or no characters, and then the row of P.

A | B | C

Match A, B, or C. For example, grep |SedMatch grep or SED

()

Group symbols, such as: Love (able | RS) ov + matches loveable or lovers and matches one or more ov.

X {m}, X {M,}, X {m, n}

Same role as X {m}, X {M,}, X {m, n}

4. POSIX character class

To retain one character in character encoding in different countries, POSIX (the Portable Operating System Interface) adds special character classes, such as [:LnUm:] 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:]

Character

[: Alpha:]

Character

[: DiGit:]

Numeric characters

[: Graph:]

Non-empty characters (non-space and control characters)

[: Lower:]

Lowercase characters

[: Cntrl:]

Control characters

[: Print:]

Non-empty characters (including spaces)

[: PUNCT:]

Punctuation Marks

[: Space:]

All blank characters (new line, space, Tab)

[: Upper:]

Uppercase characters

[: Xdigit:]

Hexadecimal number (0-9, A-F, A-F)

5. grep Command Options

-?

At the same time, the matching rows are displayed up and down? Line. For example, when grep-2 pattern filename is the same, the upper and lower rows of matching rows are displayed.

-B, -- byte-offset

Print the block number of the row before the matching row.

-C, -- count

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

-F file, -- file = File

Extract templates from files. The empty file contains 0 templates, so nothing matches.

-H, -- no-filename

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

-I, -- ignore-case

The differences between upper and lower case are ignored.

-Q, -- quiet

Cancel display. Only the exit status is returned. 0 indicates that the matched row is found.

-L, -- files-with-matches

Print the list of files matching the template.

-L, -- files-without-match

Print the list of files that do not match the template.

-N, -- line-Number

Print the row number before the matched row.

-S, -- silent

The error message about the nonexistent or unreadable file is not displayed.

-V, -- revert-match

Only unmatched rows are displayed.

-W, -- word-Regexp

If it is referenced by <and>, the expression is used as a word search.

-V, -- version

Displays the software version number.

6. Instance

To make good use of the grep tool, we actually need to write the regular expression of the regular expression, so here we are not correct about all the functions of grep for example, just a few examples are listed, describe the writing of a regular expression.

$ LS-L | grep \ '^ \'

Filter the LS-L output content in the MPs queue and only display the rows starting with.

$ Grep \ 'test \ 'd *

Displays the lines of test in all files starting with D.

$ Grep \ 'test \ 'aa BB CC

The row Matching Test is displayed in the AA, BB, and CC files.

$ Grep \ '[A-Z] {5} \ 'aa

Display All rows of strings that contain at least five consecutive lower-case characters.

$ Grep \ 'W (ES) T. * 1 \ 'aa

If West is matched, ES is stored in the memory, marked as 1, and then searched for random characters (. *). These characters are followed by another ES (1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to use the "" number for escape. You can directly write it as \ 'W (ES) T. * 1.

 

Link: http://www.linuxso.com/command/grep.html

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.