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
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
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.
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.
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.