[英]BASH: replacing PERL with SED for in-place substitution
想要將此語句替換為perl:
perl -pe "s|(?<=://).+?(?=/)|$2:80|"
與
sed -e "s|<regex>|$2:80|"
由於sed具有功能不那么強大的正則表達式引擎(例如,它不支持環顧四周),因此該任務歸結為編寫與sed兼容的正則表達式,以僅匹配完全限定URL中的域名。 例子:
http://php2-mindaugasb.c9.io/Testing/JS/displayName.js
http://php2-mindaugasb.c9.io?a=Testing.js
http://www.google.com?a=Testing.js
應該變成:
http://$2:80/Testing/JS/displayName.js
http://$2:80?a=Testing.js
http://$2:80?a=Testing.js
這樣的解決方案是可以的:
sed -e "s|<regex>|http://$2:80|"
謝謝 :)
使用以下sed命令。
$ sed "s~//[^/?]\+\([?/]\)~//\$2:80\1~g" file
http://$2:80/Testing/JS/displayName.js
http://$2:80?a=Testing.js
http://$2:80?a=Testing.js
您必須在替換部分轉義$
。
sed 's|http://[^/?]*|http://$2:80|' file
輸出:
http://$2:80/Testing/JS/displayName.js http://$2:80?a=Testing.js http://$2:80?a=Testing.js
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.