user82324
user82324

Reputation: 79

search and replace in files on linux (regex)

I have about 50 html files and need to search and replace image resizer urls in all of them

current url is like this : http://www.test1.com/showimage.php?fileUrl=/uploads/images/5x6a6s9d7a9s7d8a9.jpg&mode=resizeByMinSize,cropToSize&cropPosition=topleft&width=64&height=64esizeByMinSize,cropToSize&cropPosition=topleft&width=64&height=64

What I want is :

1:
find : http://www.test1.com/showimage.php?fileUrl=
replace with : /resizer/phpThumb.php?src=

2:
remove : &mode=resizeByMinSize,cropToSize&cropPosition=topleft

3:
find : &width=
replace with : &w=

4:
find : &height=
replace with : &h=

5:
add this to end of the image url : &far=C&q=85&zc=C

edit:
output for this sample url should be :
/resizer/phpThumb.php?src=/uploads/images/5x6a6s9d7a9s7d8a9.jpg&w=64&h=64&far=C&q=85&zc=C

Thanks

Upvotes: 1

Views: 1496

Answers (2)

Fredrik Pihl
Fredrik Pihl

Reputation: 45634

There is probably a typo in your url above, in point 2 you say to remove &mode=resizeByMinSize,cropToSize&cropPosition=topleft but you forget to mention what to do with esizeByMinSize,cropToSize&cropPosition=topleft...

Anyway, the awk scrip below solves the problem: tweek it to your needs:

# replace 'www' below with a better pattern
/www/ {
    sub(/http:\/\/www\.test1\.com\/showimage\.php\?fileUrl=/, "/resizer/phpThumb.php?src=")

    gsub(/&mode=resizeByMinSize,cropToSize&cropPosition=topleft/, "")
    gsub(/&width=/, "\\&w=")
    gsub("&height=", "\\&h=")
    $0 = $0 "&far=C&q=85&zc=C"
    print
}

quoting is a bit messy, see awk-manual Wrap this in a findsequence and your problem is solved.

Upvotes: 1

Mutant Bob
Mutant Bob

Reputation: 3549

I'm going to assume your sample URL was missing a fragment in the middle. I think the following sed script might serve your needs:

sed -e 's-http://www.test1.com/showimage.php?fileUrl=-/resizer/phpThumb.php?src=-; s/&mode=resizeByMinSize,cropToSize&cropPosition=topleft//; s/&width=/\&w=/g; s/&height=/\&h=/g; s/$/\&far=C\&q=85\&zc=C/;' /tmp/y.txt

Upvotes: 3

Related Questions