Grep: The asterisk (*) doesn't always work

Trae

If I grep a document that contains the following:

ThisExampleString

...for the expression This*String or *String, nothing is returned. However, This* returns the above line as expected.

Whether the expression is enclosed in quotes makes no difference.

I thought the asterisk indicated any number of unknown characters? Why does it only work if it's at the start of the expression? If this is intended behavior, what do I use instead of the expressions This*String and *String?

Sergiy Kolodyazhnyy

An asterisk in regular expressions means "match the preceding element 0 or more times".

In your particular case with grep 'This*String' file.txt, you are trying to say, "hey, grep, match me the word Thi, followed by lowercase s zero or more times, followed by the word String". The lowercase s is nowhere to be found in Example, hence grep ignores ThisExampleString.

In the case of grep '*String' file.txt, you are saying "grep, match me the empty string--literally nothing--preceding the word String". Of course, that's not how ThisExampleString is supposed to be read. (There are other possible meanings--you can try this with and without the -E flag--but none of the meanings are anything like what you really want here.)

Knowing that . means "any single character", we could do this: grep 'This.*String' file.txt. Now the grep command will read it correctly: This followed by any character (think of it as selection of ASCII characters) repeated any number of times, followed by String.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related