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.