I have a very basic confusion about grep. Suppose I have a following file to grep in:
test.txt:
This is an article
from some newspaper
Article is good
newspaper is not.
Now if I grep with following expression
grep -P "is\s*g" test.txt
I get the line:
Article is good
However if I do this:
grep -P "is*g" test.txt
I don’t get anything. My question is since asterix (*) is a wildcard which represents 0 or more repetitions of the previous character, shouldn’t the output of grep be the same. Why the zero or more repetitions of ‘s’ is not giving any output?
What am I missing here. Thanks for the help!
Because there’s nothing in your input that matches
i, then 0 or more repetitions ofs, theng. “Article is good” can’t match because it has a space after thes, not ag. The patternis\s*gmatches because\sis a special pattern that matches any sort of whitespace — so the overall pattern isis, then any amount of space, theng, which naturally matches “is g”.