anish
anish

Reputation: 7412

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

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

e.g dollar file contains

string 1Mcl0c41$

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 \$

Upvotes: 1

Views: 1887

Answers (1)

winstaan74
winstaan74

Reputation: 1131

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

Upvotes: 3

Related Questions