简体   繁体   English

在linux(regex)上搜索并替换文件

[英]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 我大约有50个html文件,并且需要在所有文件中搜索和替换图像缩放器网址

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 当前网址是这样的: http : //www.test1.com/showimage.php? fileUrl=/uploads/images/5x6a6s9d7a9s7d8a9.jpg&mode=resizeByMinSize,cropToSize&cropPosition=topleft&width=64&height=64esizeByMinSize,cropToSize&crop= 64

What I want is : 我想要的是:

1: 1:
find : http://www.test1.com/showimage.php?fileUrl= 查找: http : //www.test1.com/showimage.php?fileUrl=
replace with : /resizer/phpThumb.php?src= 替换为:/resizer/phpThumb.php?src=

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

3: 3:
find : &width= 找到:&width =
replace with : &w= 替换为:&w =

4: 4:
find : &height= 找到:&height =
replace with : &h= 替换为:&h =

5: 5:
add this to end of the image url : &far=C&q=85&zc=C 将此添加到图片网址的末尾:&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 /resizer/phpThumb.php?src=/uploads/images/5x6a6s9d7a9s7d8a9.jpg&w=64&h=64&far=C&q=85&zc=C

Thanks 谢谢

I'm going to assume your sample URL was missing a fragment in the middle. 我将假设您的示例URL中间缺少一个片段。 I think the following sed script might serve your needs: 我认为以下sed脚本可能会满足您的需求:

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

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 ... 上面的网址中可能有一个错字,在第2点中,您说要删除&mode=resizeByMinSize,cropToSize&cropPosition=topleft但您忘了提及如何处理esizeByMinSize,cropToSize&cropPosition=topleft ...

Anyway, the awk scrip below solves the problem: tweek it to your needs: 无论如何,下面的awk脚本可以解决问题:根据您的需求进行处理:

# 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 find sequence and your problem is solved. 引用有点混乱,请参阅awk-manualfind顺序将其包装起来,即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM