0

I am parsing logs and grabbing output, but I want to clean up the out put. currently I am getting the full path of the log file in the output which is long and not needed. The code below is what I am trying to use, but I am getting an error that my replace is not a valid regular expression.

select-string $_ -pattern "gam.exe : Error 409: Entity already exists" -context 1,0 | ForEach-Object{$_ -replace "D:\test1\user\mail\mail-Load\logs","" }

I am not sure what I need to do to get the input for replace right, do I need to escape characters?

2
  • Have you tried using single quotes instead of double? Commented Mar 25, 2014 at 16:15
  • I just did, but it did not seem to change the error. it does not like "D:\test1\user\mail\mail-Load\logs","" or 'D:\test1\user\mail\mail-Load\logs',"" Commented Mar 25, 2014 at 16:24

1 Answer 1

1

The backslash is the regex escape character. To use it as part of a literal match in the regex, it must be escaped with another backslash. If you're not sure what characters need to be escaped, [regex] has an escape method you can use that will escape all the reserved characters for you:

[regex]::escape("D:\test1\user\mail\mail-Load\logs")
D:\\test1\\user\\mail\\mail-Load\\logs

So

 'D:\\test1\\user\\mail\\mail-Load\\logs' 

is the regex you would use to match the literal string

'D:\test1\user\mail\mail-Load\logs'
Sign up to request clarification or add additional context in comments.

1 Comment

that worked, I just needed to escape the \ and it is now filtering properly. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.