Weird grep results

Source: Internet
Author: User
The original intention is to find the line with the IP address 1.1.1.1 In the file (1.1.1.191 is not required ). but I wrote an error and forgot ". "is used to match any character. however, it is found that there are several grep results that are very strange.

According to manual of GNU grep
"" Match the empty string at the end of Word

So I wrote grep "" and the result is as follows:
KSIM @ mxgms3: ~> Echo "1.1.1.1 games1" | grep ""
1.1.1.1 games1
KSIM @ mxgms3: ~> Echo "1.1.1.191 games1" | grep ""
1.1.1.191 games1

The execution result of the second sentence is beyond my expectation. As a reminder, "." matches any character.

So I changed it to grep ""
The execution result is as follows:
KSIM @ mxgms3: ~> Echo "1.1.1.191 games1" | grep ""
KSIM @ mxgms3: ~> Echo "1.1.1.1 games1" | grep ""
1.1.1.1 games1

Later, you can change it to-W: grep-W "1.1.1.1"
The execution result is as follows:
KSIM @ mxgms3: ~> Echo "1.1.1.191 games1" | grep-W "1.1.1.1"
KSIM @ mxgms3: ~> Echo "1.1.1.1 games1" | grep-W "1.1.1.1"
1.1.1.1 games1

However, I still don't understand the output result of the earliest command, which is:
KSIM @ mxgms3: ~> Echo "1.1.1.191 games1" | grep ""
1.1.1.191 games1
Why is 1.1.1.191 displayed ., ". "is used to represent an arbitrary character, but only represents an arbitrary character, cannot represent multiple, how can this be matched 1.1.1.191. similarly, I don't understand how to change to-W option:
KSIM @ mxgms3: ~> Echo "1.1.1.191 games1" | grep-W "1.1.1.1"
1.1.1.191 games1

But this should be related to the "." operator, because I tried the following command again:
KSIM @ mxgms3: ~> Echo "121212121 games1" | grep ""
KSIM @ mxgms3: ~> Echo "1212121 games1" | grep ""
1212121 games1
If you replace "." With 2, 121212121 is not displayed.
So all the problems come down to one problem: "" and-W "1.1.1.1"
Aren't these two exact matches "four 1 s and four 1 s with three arbitrary characters "??
Where is the problem?

View GNU manul and see the following section:
-W, -- word-Regexp
Select only those lines containing matches that form whole words. the test is that the matching substring must either be at the beginning of the line, or preceded by a non-word con-stituent character. similarly, it must be either at the end of the line or followed by a non-word constituent character. word-constituent characters are letters, digits, and the underscore.

That is to say,-w only cares about constituent characters. For example, other symbols do not affect the matching result. For example:
KSIM @ mxgms3: ~> Echo "+ ABC ++" | grep-W "ABC"
+ ABC ++
KSIM @ mxgms3: ~> Echo "+ ABC +" | grep-W "ABC"
+ ABC +
KSIM @ mxgms3: ~> Echo "+ AC + ABC +" | grep-W "ABC"
+ AC + ABC +
KSIM @ mxgms3: ~> Echo "+ ABC + AC +" | grep-W "ABC"
+ ABC + AC +

In addition, we found that "" has the same effect as-W:
KSIM @ mxgms3: ~> Echo "+ ABC + AC +" | grep ""
+ ABC + AC +

Let us assume that the two are equivalent.

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.