Shell Learning--grep1

Source: Internet
Author: User

I. SOURCES of grep

To find a string by using the EX Editor:

:/pattern/p

where P is printed, the first line containing the string pattern is printed, if you need to print all lines that contain pattern, you can:

:/g/pattern/p

When G appears in front of the pattern, it means finding all the rows or replacing all the rows.

The pattern that is searched is called a regular expression, regular expression, so replacing pattern with shorthand re is:

: g/re/p

This is the origin of grep, which means "global lookup of regular expressions and printing result rows"

Two. how grep works

1. Command format

grep word filename FileName:

Where word is the string pattern we are looking for, and all words after the pattern are treated as filenames.

When the pattern is found, the row of the result is printed to the screen, and the entire process does not modify any files.

2. Input file

The input of grep can come from standard inputs or pipelines, not just files.

For example:

(1) Pipeline input

Ps-ef | grep root

(2) Standard input

grep root (\ n)

Test root Administrator

3. Meta-characters and options

The meta characters that grep supports are as follows:

^: Beginning of Line locator

$: Line End Locator

.: Matches a single character

*: Match 0 or more

[]: Matches any one of a set of characters

[X-y]: match a character in the set range

[^]: matches characters not in the specified range

\: The transpose character

\<: Word head Locator

\>: Suffix locator

\(.. \): Match the label of the character that will be used later

X\{m\} or X\{m,\} or x\{m,n\}: The repetition of the character x: M times, at least m times, at least m times but not more than n times

The options commonly used by grep include:

-B precede each line with its block number, which may be used when locating a disk block based on the context

-C Displays the number of rows matched to and does not display the contents of the row

-H does not display file names

-I ignore case differences when comparing characters

-l lists only the file names where the matching rows are located, each file name is listed once, and the file name is separated by a newline character

-n precede each line with its relative line number in the file

-S silent operation, only display error messages to check the exit status

-V Reverse lookup, showing only rows that do not match

-W puts the expression as a word to the socket, as if it were contained by \< and \>.

4. grep's exit status

When grep exits, it returns a state to indicate whether to navigate to the pattern or file that needs to be looked up.

If the pattern is found, grep exits with a status of 0, indicating success, otherwise returning 1;

When grep does not find the established search file, GREP returns status 2.

After performing the grep lookup, the "echo $?" can be passed in Sh. To see its rollout status.

Shell Learning--grep1

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.