Powershell select-string fails due to escape sequence

泄露秘密 提交于 2020-01-16 07:24:32

问题


I am having a file which has following contents

change sets:
  promotion level: INITIAL
  depends on: 
    GEESF_R2.1.0.9.5179@\My_PVOB (MES@\My_PVOB)
    My_2.1.0.13.4875@\My_PVOB (Notification@\My_PVOB)
    MyComponents_8_8_2011.6859@\My_PVOB (SQLReporting@\My_PVOB)
    My_2.1.0.13.7098@\My_PVOB (Support@\My_PVOB)

I wanted to read the contents which has pattern @\My_PVOB)

So i write select-string option like this.

Select-string -pattern "@\My_PVOB)" -path "C:\Baselines.txt"

But i am getting following issue

parsing "@\My_PVOB)" - Unrecognized escape sequence \M.

Even if change the pattern as

Select-string -pattern "@\\My_PVOB)" -path "c:\Baselines.txt"

I am getting following error

 "@\\My_PVOB)" - Too many )'s.

Any idea how to solve it?


回答1:


The round bracket is a special character, so you should escape it:

Select-string -pattern "@\\My_PVOB\)" -path "c:\Baselines.txt"



回答2:


You can use the Escape method to replace metacharacters with their escape codes:

PS> $pattern = [regex]::Escape('@\My_PVOB)')
PS> $pattern
@\\My_PVOB\)

PS> Select-String -Path c:\Baselines.txt -Pattern $pattern 


来源:https://stackoverflow.com/questions/8573357/powershell-select-string-fails-due-to-escape-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!