开发者

Groovy - Strings with "$" replacing it by \$

开发者 https://www.devze.com 2023-04-09 17:52 出处:网络
How can i replace a file that contain $ to \\$ e.g dollar filecontains string 1Mcl0c41$ 开发者_如何学JAVA

How can i replace a file that contain $ to \$

e.g dollar file contains

string 1Mcl0c41$
开发者_如何学JAVA

after replacing it should look like

string 1Mcl0c41\$

using sed i can perform

$ cat dollar
string 1Mcl0c41$

$ sed "s/1Mcl0c41/1Mcl0c41\\\\/g" dollar > fixed-filename

$ cat fixed-filename
string 1Mcl0c41\$

the same i wan't to achieve by using groovy i want to replace all the occurence of $ with \$


In a groovy script / program, you can say

new File('./fixed-filename') <<  new File('./dollar').text.replace('$','\\$')

Or, from the commandline, try

groovy -e "line.replace('$','\\\\$')" -p dollars > fixed-filename
0

精彩评论

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

关注公众号