Reputation: 11
I am trying to search a keyword/pattern match in a file, where the lines will be starting with date. Line will be like below
11/02/15 02:28:49%%PROGRAM$$SUCCESS$$End.
So i tried with below command,
Select-String -Path C:\Path\To\File.txt -Pattern $(Get-Date -format d) | Select-String -Pattern SUCCESS
So that i can get lines which contain SUCCESS
with a starting of current date.
Its working on my test box and when i tried the same on a big file (~200 MB), its not giving any results. Tried below too,
Get-Content -Path C:\Path\To\File.txt | Select-String -Pattern $(Get-Date -format d) | Select-String -Pattern SUCCESS
Any help any help would be greatly appreciated!
Upvotes: 1
Views: 99
Reputation: 47862
Some things to consider here. As PetSerAl brings to light, Get-Date -Format d
depends on the culture, so you need to be careful about relying on the output of that.
If the files you're searching are generated using Get-Date -Format d
then it makes sense to do the search that way as long as the files will always be searched on a machine with the same culture they were generated with.
By the way on my machine it's 11/2/2015
not 11/02/15
and I am in the US.
Also, when you use Select-String -Pattern
it's a regular expression, so you need to make sure that there are no special characters in the string. In the case of PetSerAl's date, the dots .
would be interpreted as special characters. To avoid that use [RegEx]::Escape()
.
Select-String
returns a match object (or objects), so piping it directly into another Select-String
may not work. Consider making a single pattern out of it.
Just a guess here, but it kind of seems like the pattern you want is to match the current date string at the beginning of the line and then find SUCCESS
anywhere after that in the line.
I think for that you could use a pattern like this: 11/02/15.+?SUCCESS
So code like this:
Get-Content -Path C:\Path\To\File.txt | Select-String -Pattern "$([RegEx]::Escape((Get-Date -Format d))).+?SUCCESS"
Would do the trick I think, again assuming culture issues don't mess you up.
Upvotes: 1