Home How to replace a text string with dollar sign $ in Linux?
Reply: 3

How to replace a text string with dollar sign $ in Linux?

A. Gupta
1#
A. Gupta Published in 2018-02-14 05:50:36Z

I am trying to replace a text '../../Something' with '$Something' in all .txt files in current directory.Let me know where I am going wrong?

find . -name "*.txt" | xargs sed -i "s/..\/..\/Something/\'\$Something'/g"

Error - Variable name must contain alphanumeric character

I also tried with but doesn't work-

find . -name "*.txt" | xargs sed -i "s/..\/..\/Something/\\$Something/g"

Any suggestions for correct command?

Hargobind
2#
Hargobind Reply to 2018-02-14 06:05:51Z

You're shell is treating the $ as the start of a variable.

There are two ways you can make it work:

  1. Use single quotes, which tells the shell to not perform any variable interpolation (among other things):

    find . -name "*.txt" | xargs sed -i 's/..\/..\/Something/\$Something/g'

  2. Escape the $ from the shell and sed. This requires 3 backslashes (the first one escapes the second backslash, the second escapes the dollar sign once the tring reaches sed, and the third escapes the dollar sign in the shell so it doesn't get treated as a variable):

    find . -name "*.txt" | xargs sed -i s/..\\/..\\/Something/\\\$Something/g

nu1silva
3#
nu1silva Reply to 2018-02-14 06:04:39Z

Tested the following and it worked for me. Let me know if this works

find . -name "*.txt" | xargs sed -i "s,../../Something,$\Something,g"
Diksha P
4#
Diksha P Reply to 2018-02-14 06:16:57Z

You can try removing the extra backslash given in the second command.

find . -name "*.txt" | xargs sed -i 's/../../Something/\$Something/g'

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.428658 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO