mostworld77
mostworld77

Reputation: 427

sed replacing without untouching a string

Im trying to replace all lines within files that contains:

/var/www/webxyz/html

to

/home/webxyz/public_html

the string: webxyz is variable: like web1, web232

So only the string before and after webxyz should be replaced. Tried this without solution:

sed -i 's/"var/www/web*/html"/"home/web*/public_html"/g'

Also i want this should check and replace files (inclusive subdirectory and files), the * operator don't work.

Upvotes: 0

Views: 73

Answers (2)

Gumbo
Gumbo

Reputation: 655269

Within a regular expression, you’ll need to escape the delimiting character that surround them, in your case the /. But you can also use a different delimiter like ,:

sed -i 's,"var/www/web*/html","home/web*/public_html",g'

But to get it working as intended, you’ll also need to remove the " and replace the b* (sed doesn’t understand globbing wildcards) to something like this:

sed -i 's,var/www/web\([^/]*\)/html,home/web\1/public_html,g'

Here \([^/]*\) is used to match anything after web except a /. The matching string is then referenced by \1 in the replacement part.

Upvotes: 3

abiessu
abiessu

Reputation: 1927

Here is what your replacement operation should look like (see sed for more info):

sed -i 's/var\/www\(\/.*\/\)html/home\1public_html/g'

Note that \(...\) is a grouping, and specifies a "match variable" which shows up in the replacement side as \1. Note also the .* which says "match any single character (dot) zero or more times (star)". Note further that your / characters must be escaped so that they are not treated as part of the sed control structure.

Upvotes: 1

Related Questions