Linuxgrep command details

Source: Internet
Author: User
Tags egrep
Article Title: linuxgrep command details. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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.

[1] [2] Next page

Related Article

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.