aquemini
aquemini

Reputation: 960

Select-String pattern not matching

I have the text of a couple hundred Word documents saved into individual .txt files in a folder. I am having an issue where a MergeField in the Word document wasn't formatted correctly, and now I need to find all the instances in the folder where the incorrect formatting occurs. the incorrect formatting is the string \#,$##,##0.00\* So, I'm trying to use PowerShell as follows:

select-string -path MY_PATH\.*txt -pattern '\#,$##,##0.00\*'
select-string -path MY_PATH\.*txt -pattern "\#`,`$##`,##0.00\*"

But neither of those commands finds any results, even though I'm sure the string exists in at least one file. I feel like the error is occurring because there are special characters in the parameter (specifically $ and ,) that I'm not escaping correctly, but I'm not sure how else to format the pattern. Any suggestions?

Upvotes: 0

Views: 913

Answers (2)

Matt
Matt

Reputation: 46690

If you are actually looking for \#,$##,##0.00\* then you need to be aware that Select-String uses regex and you have a lot of control characters in there. Your string should be

\\\#,\$\#\#,\#\#0\.00\\\*

Or you can use the static method Escape of regex to do the dirty work for you.

[regex]::Escape("\#,$##,##0.00\*")

To put this all together you would get the following:

select-string -path MY_PATH\.*txt -pattern ([regex]::Escape("\#,$##,##0.00\*"))

Or even simpler would be to use the parameter -SimpleMatch since it does not interpet the string .. just searches as is. More here

select-string -path MY_PATH\.*txt -SimpleMatch "\#,$##,##0.00\*"

Upvotes: 3

Micky Balladelli
Micky Balladelli

Reputation: 9991

My try, similar to Matts:

select-string -path .\*.txt -pattern '\\#,\$##,##0\.00\\\*'

result:

test.txt:1:\#,$##,##0.00\*

Upvotes: 1

Related Questions