0

I know that for replacing a string (in a file which has matchstring string), we can use following command
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
How can I use/change the command if my string has special characters like "/"?
For example:
string1: "/home/folder1"
string2: "/home/folder1/folder2"

Shannon
  • 985
  • 3
  • 11
  • 25
  • 1
    You can use (almost) any delimiter character, for example 's,string1,string2,g'. [What delimiters can you use in sed?](https://stackoverflow.com/questions/33914360/what-delimiters-can-you-use-in-sed) – jamieguinan Mar 24 '18 at 01:32
  • @jamieguinan, I did not know that. It worked! Thanks a lot. – Shannon Mar 24 '18 at 01:37
  • also note that filenames having characters like space can cause issue.. use `grep -rlZ 'matchstring' somedir/ | xargs -0 ` – Sundeep Mar 24 '18 at 03:26

1 Answers1

0

As @jamieguinan mentioned in his command, almost any delimiter character can be used. So, I changed the command as following:
grep -rl matchstring somedir/ | xargs sed -i 's,string1,string2,g'
Where string1 and string2 are: /home/folder1 and /home/folder1/folder2, respectively.

Shannon
  • 985
  • 3
  • 11
  • 25