gaggina
gaggina

Reputation: 5425

Regex in sed to replace parts of an url given a specific format

I'm having some issues in doing a simple regex using sed.

I've to do some replacement in a sql file and I'm trying to use sed.

I should replace the url of some links. The links are in the following format:

www.site1.com/blog/2012/12/12

I would like to replace site1 with site2 in all links.

To find these links I've written the following regex:

(site1.com)\/blog\/\d{4}\/\d{2}\/\d{2}

And seems to wokr properly.

Using sed to do the replacement things I've written the following code

cat back.sql | sed 's:(site1.com)\/blog\/\d{4}\/\d{2}\/\d{2}:site2.com:' > fixed.sql

But it seems is not working..

Upvotes: 1

Views: 1445

Answers (2)

knittl
knittl

Reputation: 265211

sed does not support \d (not to my knowing at least), and supports {4} only with extended regular expressions.

sed -r 's:site1.com(/blog/[0-9]{4}/[0-9]{2}/[0-9]{2}):site2.com/\1:'

as a basic regular expression (requires lots of escaping):

sed 's:site1.com\(/blog/[0-9]\{4\}/[0-9]\{2\}/[0-9]\{2\}\):site2.com/\1:'

ps. you don't need to escape slashes if you use different delemiters (:)

Upvotes: 5

user571138
user571138

Reputation:

Looks to be a straight substitution to me:

$ sed -i s/\.site1\./\.site2\./g afile.txt

... where afile.txt contains your list of sites.

If you want to output to another file, remove the -i and redirect the output using > .

Upvotes: 0

Related Questions