grep command parameters and how to use them

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

Feature Description: Find files that match the conditions of the strings.

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

Supplemental Note: Thegrep directive is used to find files that contain the specified template style, assuming that the contents of a file conform to the specified template style, and that the preset grep directive will display the column containing the template style. If you do not specify whatever file name is called. or the given file name is "-", the grep instruction reads the data from the standard input device.

Number of references:
-A or--text do not ignore binary data.
-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.
-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 the column.
-d< action > or--directories=< action > When you specify that you want to find a folder rather than a file. This parameter must be used, otherwise the grep command 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 between uppercase and lowercase characters.


-L or--file-with-matches lists file names that conform 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 whatever information.
The effect of-R or--recursive 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 the version number information.
-W or--WORD-REGEXP displays only the columns that match the whole word.
-X or--LINE-REGEXP displays only the columns that are eligible for all columns.
-Y the effect of this parameter is the same as specifying the "-i" parameter.
--help online Help.

Use of the Linux grep command
Search for text files with the grep command from www.linuxso.com

Suppose you want to find a string in a few text files and be able to use the &lsquo;grep ' command. ' grep ' searches the text for the specified string.
If you are searching for a file with the string ' magic ' under the '/usr/src/linux/documentation ' folder:
$ grep magic/usr/src/linux/documentation/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* How does I use the Magic sysrq key?

The file ' Sysrp.txt ' includes the string, which discusses the function of SYSRQ.
By default, ' grep ' searches only the current folder.

Assuming there are many subfolders under this folder, ' grep ' is listed in the following form, for example:
Grep:sound:Is a Directory
This may make the output of ' grep ' difficult to read. There are two ways to solve this problem:
Understand requirements Search subfolders: Grep-r
or Ignore subfolders: grep-d Skip
Of course, assuming that there is a lot of output, you can go through the pipelineto read it on ' less '
$ grep magic/usr/src/linux/documentation/* | Less
This makes it easier for you to read.
One thing to note. You must provide a way to filter the file (search for all files in the words *). Suppose you forget, ' grep ' will wait until the program is interrupted. If you encounter this situation, press <ctrl c>, and then try again.
Here are some interesting command-line parameters:
Grep-i pattern Files: Search with no distinction between uppercase and lowercase. The default case is uppercase and lowercase,
Grep-l pattern Files: Lists only the matching file names,
Grep-l Pattern Files: Lists the file names that do not match.
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic '). Instead of ' 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.


Here are some special symbols for searching:
< and > mark the beginning and end of each word.


Like what:
grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep \ ' <man\ ' matches ' manic ' and ' man '. But not ' Batman '.
grep \ ' <man>\ ' only matches ' man ', not ' Batman ' or ' manic ' and other strings.


\ ' ^\ ': refers to the matching string at the beginning of the row.
\ ' $\ ': refers to a matching string at the end of the line,
Suppose you are not used to command line parameters, you can try the graphical interface ' grep ', such as Rexgrep. This software provides syntax for and, or, not, and beautiful button:-). Suppose you just need a clearer output, it's best to try Fungrep.

. grep search String
Command format:
grep string filename
There are a lot of ways to look for strings, for example, I want to find all the lines that start with M. The concept of pattern must be introduced at this time
Read on. Here are some simple-examples, as well as descriptions:
^m a line starting with M, ^ means beginning
m$ the line ending with M, $ denotes the end meaning
^[0-9] The line starting with a number, [] can be listed in the letter
^[124AB] Lines beginning with 1,2,4,a, or b
^b.503 period denotes any letter
* Asterisks indicate more than 0 letters (can not)
+ 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

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

1. grep Simple Introduction

grep (Global search regular expression (RE) and print out of the line, full search of regular expressions and print out the lines) is a powerful text search tool that can use the c2> the text, and prints the matching lines. The grep family of Unix includes grep,egrep , and fgrep.

Egrep and Fgrep commands are only very small differences from grep. Egrep is a grep extension that supports many other re-metacharacters. Fgrep is fixed grep or fast grep, which regards all the letters as words, that is to say, the metacharacters in the form of the expression returns to its own literal meaning, no longer special. Linux uses the GNU version number of grep.

It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options.

The way grep works is this, which searches for a string template in one or more files. Assuming that the template contains 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 can be used for Shell scripting, assuming that the template search succeeds by returning a status value to describe the status of the search. Returns 0, assuming the search is unsuccessful. Returns 1, assuming that the searched file does not exist, 2 is returned. We can use these return values to do some of our own proactive text processing work.


2. grep is the form of the meta-character set (Basic set)

^

The start of the anchoring line is as follows: \ ' ^grep\ ' matches all rows that begin with grep.

$

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

Match a non-newline character such as: \ ' gr.p\ ' matches the GR followed by a random char followed by P.

*

Match 0 or more of the preceding characters such as: \ ' *grep\ ' matches all one or more spaces followed by the line of grep.

. * Used together to represent random characters.

[]

Matches a character within 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 include A-r and t-z. The line immediately following the Rep.

(..)

Tag matches the character. Like \ ' (Love) \ ', Love is marked as 1.

<

Anchor the beginning of a word, such as: \ '

>

Anchor the end of the word. such as \ ' grep>\ ' matches lines that include words ending with grep.

X{M}

Repeated characters x,m times. such as: \ ' 0{5}\ ' matches a row that includes 5 O.

X{m,}

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

X{m,n}

Repeated characters x, at least m times, no more than n times. such as: \ ' o{5,10}\ ' matches the line of 5--10 O.

W

Matches literal and numeric characters. That is, [a-za-z0-9], such as: \ ' gw*p\ ' matches a G followed by 0 or more literal or numeric characters. And then the p.

W

The inverted form of w, matching one or more non-word characters. such as the dot period, and so on.

B

The word lock, such as: \ ' bgrepb\ ' just matches grep.

3. 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

Match 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. Match one or more ov.

X{m},x{m,},x{m,n}

function with X{m},x{m,},x{m,n}

4. POSIX character class

POSIX (The Portable Operating System Interface) adds special character classes for the character encodings in different countries, such as [: alnum:] is a-za-z0-9 and has a notation.

To put them in the [] number of the ability to become a regular form. such as [a-za-z0-9] or [[: Alnum:]]. grep under Linux supports POSIX character classes in addition to Fgrep.

[: Alnum:]

Literal numeric characters

[: Alpha:]

Literal characters

[:d igit:]

numeric characters

[: Graph:]

Non-null characters (non-whitespace, control characters)

[: Lower:]

lowercase characters

[: Cntrl:]

Control characters

[:p rint:]

Non-null characters (including spaces)

[:p uNCT:]

Punctuation

[: Space:]

All whitespace characters (new lines). Spaces, tabs)

[: Upper:]

Uppercase characters

[: Xdigit:]

hexadecimal digits (0-9,a-f,a-f)

5. grep command Options

-?

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 at the same time.

-B. --byte-offset

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

-C,--Count

Prints only the number of matching rows. The matching content is not displayed.

-F File,--file=file

Extract the template from the file.

There are 0 templates in the empty file, so nothing matches.

-h,--no-filename

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

-I. --ignore-case

Ignores uppercase and lowercase 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, showing only rows that do not match.

-W. --word-regexp

Assumptions are referenced by < and >. The expression is searched as a single word.

-V. --version

Displays the software version number information.

6. Example

To use grep as a tool, the fact is to write a good form. So here is not the correct grep all the functions of the illustrative example, just a few examples, explaining a regular form of the notation.

$ 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 the lines that contain test in all files that begin with D.

$ grep \ ' test\ ' AA bb cc

Displayed in AA,BB. The line in the CC file that matches the test.

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

Displays a line that includes all strings 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 mark it as 1, then search for random characters (. *). These characters are followed by another ES (1), which is found to display the line. Suppose Egrep or grep-e, do not "" There is no way to escape. Write directly \ ' W (es) t.*1\ ' it.

Original link: http://www.linuxso.com/command/grep.html

grep command parameters and how to use them

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.