0

So not sure, but i tried the following and it didn't work.
What string can i use to replace a directory string in sed?

sed 's"/usr/lib64/$id""/home/user1/$id"/g' 1.php > 1_new.php
Jotne
  • 40,548
  • 12
  • 51
  • 55
user3100345
  • 41
  • 10
  • 1
    Related: [How to replace a path with another path in sed?](http://stackoverflow.com/q/12061410/1258041) and [Replace a string in shell script](http://stackoverflow.com/questions/3306007/replace-a-string-in-shell-script). – Lev Levitsky May 23 '14 at 19:54

1 Answers1

0

Because your strings have slashes in them, it is convenient to use a different separator in the substitution expression. Here, I use | as the separator. If, for simplicity, we ignore the double-quotes inside your regular expression, we could use:

sed "s|/usr/lib64/$id|/home/user1/$id|g" 1.php > 1_new.php

If we include the double-quotes, then quoting because a bit more complex:

sed 's|"/usr/lib64/'"$id"'"|"/home/user1/'"$id"'"|g' 1.php > 1_new.php

I assume that you intend for $id to be replaced by the value of the shell variable id. If so, it is important in both cases that the expression $id not be inside single-quotes as they inhibit variable expansion.

Discussion of Quoting

The quoting in the second case may look ugly. To help explain, I will add some spaces to separate the sections of the string:

sed 's|"/usr/lib64/' "$id" '"|"/home/user1/' "$id" '"|g' # Don't use this form.

From the above, we can see that the sed command is made up of a single-quoted string, 's|"/usr/lib64/' followed by a double-quoted string, "$id", followed by a single-quoted string, '"|"/home/user1/', followed by a double-quoted string, "$id", followed by a single-quoted string, '"|g'. Because everything is in single-quotes except that which we explicitly want the shell to expand, this is generally the approach that is safest against surprises.

Alternate Quoting Style

The following quoting style may seem simpler:

sed "s|\"/usr/lib64/$id\"|\"/home/user1/$id\"|g" 1.php > 1_new.php

In the above, the sed command is all one double-quoted string. Double-quotes can be included within double-quoted strings by escaping them as \". This style is fine as long as you are confident that you know that the shell won't expand anything except what you want expanded.

John1024
  • 109,961
  • 14
  • 137
  • 171