Reputation: 2044
I have various images that are referenced as such:
src="http://myblog.files.example.com/2011/08/image-1.jpg"
src="http://myblog.files.example.com/2010/05/image-2.jpg"
src="http://myblog.files.example.com/2012/01/image-3.jpg"
As you can see the only thing that changes in the image paths are the numbers (dates) at the end. What I'd like to do is simply change the path for all images to something like:
/sites/default/files/blog-images/
... so they would all be like:
src="/sites/default/files/blog-images/image-1.jpg"
src="/sites/default/files/blog-images/image-2.jpg"
src="/sites/default/files/blog-images/image-3.jpg"
I am wondering if there is a way to do this using regular expressions or some other method? There are hundreds of images all with different numbers in the path so doing this manually is not ideal.
complete sample line of code:
<a href="http://myblog.files.example.com/2011/07/myimage-1.jpg">
<img class="alignright size-medium wp-image-423" title="the title" src="http://myblog.files.example.com/2011/07/myimage-1.jpg" alt="the alt" width="300" height="199" />
</a>
Upvotes: 0
Views: 276
Reputation: 5768
src="http:\/\/myblog.files.example.com/\d{4}/\d{2}/([^\s]+)"
Searches and captures the image file names in $1
. Now you can do a replace with /sites/default/files/blog-images/$1
If your editor doesn't support ranges then you'll need to repeat \d
.
src="http:\/\/myblog.files.example.com/\d\d\d\d/\d\d/([^\s]+)"
Upvotes: 1
Reputation: 16714
There are many ways to do what you are trying to do. Since you tagged "grep" in this post and did not specify any particular programming language, I am going to assume that you want to use UNIX.
First, test out this command:
find . -type f | sed 's/\.\/\([0-9]\{4\}\/[0-9]\{2\}\/\)\(.*\)/mv & sites\/default\/files\/blog-images\/\2/'
It will print out a list of "mv" commands that will be executed, and if it works how you want it do, you can pipe this into sh
like so:
find . -type f | sed 's/\.\/\([0-9]\{4\}\/[0-9]\{2\}\/\)\(.*\)/mv & sites\/default\/files\/blog-images\/\2/' | sh
Upvotes: 0
Reputation: 185106
A simple sed one liner can do the trick :
This one to test :
sed -r 's@http://myblog.files.example.com/[0-9]{4}/[0-9]{2}/@/sites/default/files/blog-images/@g' FILE
This one to change the file
sed -ri 's@http://myblog.files.example.com/[0-9]{4}/[0-9]{2}/@/sites/default/files/blog-images/@g' FILE
Upvotes: 0