Grep-powerful text search tool in Linux

Source: Internet
Author: User
Tags egrep

Grep Introduction

 

Grep (Global Search Regular Expression and print out the line, full search for regular expressions and print out rows) is a powerful text search tool that can use regular expressions to search for text, and print the matched rows. UNIX grep families include grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from those of grep. Egrep is an extension of grep and supports more re metacharacters. fgrep is fixed grep or fast grep. They regard all the letters as words, that is, the metacharacters in a regular expression represent the literal meaning of the regular expression. They are no longer special. Linux uses GNU grep. It is more powerful. You can use the-G,-E,-F command line options to use egrep and fgrep functions. egrep indicates "grep-e ", fgrep indicates "grep-F ".

 

How grep works

Grep works like this. 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 treated as file names. The search result is sent to the screen without affecting the content of the original file. Grep can be used in shell scripts because 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 automate text processing.

 

Grep Command Format

Grep [Command Option] pattern file

Except the command itself, it contains three parts. If no command option is included, all rows matching pattern are output (equivalent to the-H parameter, default)

 

Grep Command Parameters

-? At the same time, the upper and lower lines of matching rows are displayed? Line. For example, grep-2 pattern filename simultaneously displays the upper and lower two rows of matching rows.

-C, -- count: only the number of matched rows is printed, and the matching content is not displayed.

-I, -- ignore-case ignore case differences

-N, -- line-number: Output matched rows and display row numbers

-L, -- files-with-matches only prints the file name of the matched row, that is, only prints the file list that matches the template.

-L, -- files-without-match print the list of files that do not match the template.

-I, -- Binary-files = without-match indicates that binary files are not searched, such as. tar files.

-V, -- revert-Match: Only unmatched rows are displayed.

-F file, -- file = file: extract the template from the file. The empty file contains 0 templates, so nothing matches.

-D, -- directories = specifies the method to process the Directory, which can be "read", "recurse", or "Skip"

-D, -- devices = specifies the method for processing device files, MPs queue files, and socket files. It can be "read" or "Skip"

-R,-R, -- Recursive is equivalent to -- directories = recurse

-B, -- byte-offset: the output matches the row and displays the byte offset.

-Q, -- Quiet is not displayed. Only the exit status is returned. 0 indicates that the matched row is found.

-S, -- silent does not display error messages about nonexistent or unreadable files.

-W, -- word-Regexp if it is referenced by/<and/>, the expression is used as a word search.

-V, -- version: displays the software version.

 

-- Include = the specified file for pattern matching will be checked

-- Exclude = the specified file for pattern matching will be skipped

-- Exclude-from = the file will be skipped if it matches the specified mode.

 

 

Example:

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.