D Kamran
D Kamran

Reputation: 53

Search a pattern with spaces in unix text file

I've a pattern line (like below) in unix text file.
Tue Mar 31 00:01:10 2015
Now I want to search those lines in a file and my search criteria will be Mar 31 2015. Can I achieve this with grep.

Because my search criteria is not that simple.

Regards,

DKamran

Upvotes: 0

Views: 1567

Answers (3)

Barmar
Barmar

Reputation: 781350

Use .* in the regular expression to match anything between the date and year.

grep "Mar 31.*2015" filename

To search for is intelligent, do:

grep "is .* intelligent" filename

Upvotes: 1

Oliver Friedrich
Oliver Friedrich

Reputation: 9240

This will work with any time in the specified format in between, but not with other characters.

/Mar\s31\s[\d:]+\s2015/

You can test it here. It will print out the matching lines along with the line-number if used with grep -nP.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174736

You could try the below grep command.

grep '\bMar 31 [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} 2015\b' file

\{2\} quantifier repeats the previous token [0-9] exactly two times.

Upvotes: 0

Related Questions