Detailed explanation of grep commands in Linux

Source: Internet
Author: User
Tags egrep

The grep command is a row filtering tool in linux with a wide range of parameters. The following describes the functions of each parameter and hopes to help you.
Grep -- print lines matching a pattern (list the rows that match the style)
◎ Syntax: grep [options]
PATTERN [FILE...]
Grep is used to compare the corresponding part in the file, or when no file is specified,
The comparison is performed in the standard input. By default, grep lists the rows that match the style.
In addition, there are two programs: grep variant, egrep and fgrep.
Egrep is equivalent to grep-E, and fgrep is equivalent to grep-F.
◎ Parameters
1.-a num, -- after-context = NUM
In addition to listing the rows that match the rule, and listing the NUM rows after the rule.
Example: $ grep-A 1 panda file
(Search for rows with a panda style from the file and display the last row of the row)
2.-a or -- text
Grep was originally used to search for text files. If a binary file is used as the search target,
The following message is displayed: Binary file name matches.
If the-a parameter is added, binary files can be searched as text files,
It is equivalent to the -- binary-files = text parameter.
Example: (search for the panda style from the binary file mv)
(Error !!!)
$ Grep panda mv
Binary file mv matches
(This indicates that this file has a match. For details, see -- binary-files = TYPE)
$
(Correct !!!)
$ Grep-a panda mv
3.-B NUM, -- before-context = NUM
It is opposite to-a num, but this parameter is displayed except for conforming rows
And displays the NUM row before it.
Example: (search for rows with a panda style from the file and display the first row of the row)
$ Grep-B 1 panda file
4.-C [NUM],-NUM, -- context [= NUM]
Lists the upper and lower NUM rows that match the rows. The default value is 2.
Example: (list all rows in a file including the panda style and the top and bottom two rows)
(To change the default value, simply change NUM)
$ Grep-C [NUM] panda file
5.-B, -- byte-offset
List the total number of bytes in the text before the style ..
Exemple: $ grep-B panda file
The display result is similar:
0: panda
66: pandahuang
123: panda03
6. -- binary-files = TYPE
This parameter TYPE is set to binary by default. If you search in normal mode, there are only two results:
1. If there is a match: display the Binary file name matches
2. If there is no match: Nothing is displayed.
If the TYPE is without-match, this parameter is displayed,
Grep considers that the binary file does not contain any search style, which is the same as the-I parameter.
If TPYE is text, grep regards the binary file as a text file, which is the same as the-a parameter.
Warning: -- binary-files = text if the output is a terminal, unnecessary output may be generated.
7.-c, -- count
Only the total number of matched rows is displayed.
If-v and -- invert-match are added, the total number of non-conforming rows is displayed.
8.-d ACTION, -- directories = ACTION
If the input file is a folder, use ACTION to process the folder.
The default ACTION is read (read), which means the folder will be considered as a normal file;
If the ACTION is a skip (skipped), the folder will be skipped by grep:
If the ACTION is recurse (recursive), grep reads all files in the folder,
This is equivalent to the-r parameter.
9.-E, -- extended-regexp
Use the rule expression to explain the style.
10.-e PATTERN, -- regexp = PATTERN
Use a style as a partern, which is usually used to avoid the use of-to-start with partern.
11.-f FILE, -- file = FILE
The style to be searched is written to an archive, with one style in one row.
Then, archive search is used.
An empty file indicates that no style is to be searched, so there will be no matching.
Ex: (newfile is the search Style File)
$ Grep-f newfile file
12.-G, -- basic-regexp
The style is interpreted as a basic rule expression. (This is a preset)
13.-H, -- with-filename
Add the name of the file that matches the style line. If a path exists, the path is displayed.
Ex: (search for the panda style in file and testfile)
$ Grep-H panda file./testfile
File: panda
./Testfile: panda
$
14.-h, -- no-filename
Similar to the-H parameter, but the path is not displayed in the output.
15. -- help
Generate a short help message.
16.-I
Grep will forcibly consider that this binary file does not contain any search style,
The parameter is the same as -- binary-files = without-match.
Example: $ grep-I panda mv
17.-I, -- ignore-case
Case Insensitive, including the style to be searched and the file to be searched.
Example: $ grep-I panda mv
18.-L, -- files-without-match
The normal output result is not displayed, but the non-conforming file name is displayed.
19.-l, -- files-with-matches
The normal output results are not displayed, and only the names of the matching files are displayed.
20. -- mmap
If possible, use the mmap system call to read the input, instead of the preset read system call.
In some situations, -- mmap can produce better performance. However, -- mmap
If the archive is shortened or an I/O error occurs during operation,
May cause undefined behaviors (including core dump ),.
21.-n, -- line-number
Mark the row number before displaying the row.
Ex: $ grep-n panda file
The display result is similar to the following:
Row number: Line Content
22.-q, -- quiet, -- silent
No general output is displayed. See-s or -- no-messages
23.-R-r, -- recursive
Recursively read all files in each folder, which is equivalent to the-d recsuse parameter.
24.-s, -- no-messages
The error message about nonexistent or unreadable is not displayed.
Note: Unlike GNU grep, the traditional grep does not comply with the POSIX.2 protocol,
The-q parameter is missing, and its-s parameter table is like the-q Parameter of GNU grep.
Shell Script tends to port the traditional grep to avoid the-q and-s parameters,
Limit the output to/dev/null.
POSIX: defines the functions required by UNIX and UNIX-like systems.
25.-V, -- version
The version number of grep is displayed as a standard error.
When you return the grep bugs, the grep version number must be included.
26.-v, -- invert-match
Display All except search style rows.
27.-w, -- word-regexp
The search style is regarded as a word to be searched. Only rows that fully match the word are listed.
28.-x, -- line-regexp
The search style is regarded as a row to be searched. Only rows that completely match the "row" will be listed.


From the column andy572633

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.