user
user

Reputation: 103

GREP for multiple strings with wildcard

I am trying to copy lines from a log file from certain days.

Here's an example of what they look like.

2014-05-01T15:53:16+00:00 DEBUG (7): 

2014-04-301T11:08:10+00:00 DEBUG (7):

This GREP command works but only for exact strings:

grep -w '2014-04-30\|2014-04-29\|2014-04-28\|2014-04-27\|2014-04-26\|2014-04-25\|2014-04-24\|2014-04-23\|2014-04-22\|2014-04-21\|2014-04-202014-04-19' /test_custom.log > new_file.log

When I try to add the wildcard, it doesn't. I also tried several other ways with same result.

grep -w '2014-04-30*\|2014-04-29*\|2014-04-28*\|2014-04-27*\|2014-04-26*\|2014-04-25*\|2014-04-24*\|2014-04-23*\|2014-04-22*\|2014-04-21*\|2014-04-20*' /test_custom.log > new_file.log

Any suggestions?

Upvotes: 2

Views: 4291

Answers (1)

Rubens
Rubens

Reputation: 14768

The asterisk * is not a wildcard in grep's regex. It won't expand into a list of things varying from the last character. * stands for Kleene closure, and is meant to accept/match 0 or more occurrences of the previous character/character class.

In your case, you should add a ., which stands for accepts/matches any character. The final expression should look like:

grep -w '2014-04-30.*\|2014-04-29.*\|2014-04-28.*\|2014-04-27.*\|
    2014-04-26.*\|2014-04-25.*\|2014-04-24.*\|2014-04-23.*\|2014-04-22.*\|
    2014-04-21.*\|2014-04-20.*' /test_custom.log > new_file.log

Which, although returns the result you want, is quite tiresome to write. Therefore, you should exploit grep's regex capabilities, and try something a bit more concise, as in:

grep -w '2014-04-30.*\|2014-04-2[0-9]-.*' /test_custom.log > new_file.log

Upvotes: 2

Related Questions