简体   繁体   English

BASH:用SED替换PERL进行就地替换

[英]BASH: replacing PERL with SED for in-place substitution

Would like to replace this statement with perl: 想要将此语句替换为perl:

perl -pe "s|(?<=://).+?(?=/)|$2:80|"

with

sed -e "s|<regex>|$2:80|"

Since sed has a much less powerful regex engine (for example it does not support look-arounds) the task boils down to writing a sed compatible regex to match only a domain name in a fully qualitied URL. 由于sed具有功能不那么强大的正则表达式引擎(例如,它不支持环顾四周),因此该任务归结为编写与sed兼容的正则表达式,以仅匹配完全限定URL中的域名。 Examples: 例子:

http://php2-mindaugasb.c9.io/Testing/JS/displayName.js
http://php2-mindaugasb.c9.io?a=Testing.js
http://www.google.com?a=Testing.js

Should become: 应该变成:

http://$2:80/Testing/JS/displayName.js
http://$2:80?a=Testing.js
http://$2:80?a=Testing.js

A solution like this would be ok: 这样的解决方案是可以的:

sed -e "s|<regex>|http://$2:80|"

Thanks :) 谢谢 :)

Use the below sed command. 使用以下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

You must need to escape the $ at the replacement part. 您必须在替换部分转义$

sed 's|http://[^/?]*|http://$2:80|' file

Output: 输出:

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.

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