开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜