Grep and grep commands

Source: Internet
Author: User
Tags grep regular expression egrep

Grep and grep commands

Grep (global search regular expression (RE) and print out the line, full search for regular expressions and print out rows) is a powerful text search tool, it can use regular expressions to search for text and print matching 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 and can use egrep and fgrep functions through the-G,-E,-F command line options.
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.
2. grep Regular Expression metacharacter set (basic set)

^
For example, '^ 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' matches gr followed by any character and then p.
*
Match zero or multiple previous characters, for example :'Grep 'matches all one or more spaces followed by grep rows. .Represents any character.
[]
Matches a character in a 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 :'\

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.