开发者

Using get-content cmdlet with -replace option in order to replace an occurrence in a csv, can't figure out how

开发者 https://www.devze.com 2023-03-21 04:00 出处:网络
I need to delete all occurrences of the string \"|\" the regex I\'ve come up with is \\\"\\|\\\" which works perfectly fine, as tested on rubular, however, when attempting to insert this into my power

I need to delete all occurrences of the string "|" the regex I've come up with is \"\|\" which works perfectly fine, as tested on rubular, however, when attempting to insert this into my powershell script it doesn't seem to work, any idea why? Or, rather, how to fix i开发者_运维问答t?

Script:

(get-content "file") -replace \"\|\", "|" | out-file "file"

-Thanks


You need to escape the pipe in the regex, but not the double quotes. To include them in the regex, single-qoute the entire regex. If you just want to remove them, don't specify a replacement string.

(get-content "file") -replace '"\|"'  | out-file "file" 


you've not encapsulated the first string (the one to be replaced):

(get-content Readme.txt) -replace '"\|"', "|" | out-file out.txt

I've used ' to avoid needing to escape "

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号