在URL中添加:冒号

时间:2014-07-15 15:21:04

标签: linux shell sed

我是shell脚本的新手,我有一个这样的文本文件:

`http://example.com    http//example.net http//www.flugov/individualfamily/prevention/index.html`

我想用http取代没有冒号的http:我试过这种方式:

sed 's/http/http:/g' file

我得到的是URL中的额外冒号

http:://example.com http://example.net http://www.flugov/individualfamily/prevention/index.html

有任何解决此问题的建议。

3 个答案:

答案 0 :(得分:3)

确保:之后没有http并打印该字符:

$ sed -r 's/http([^:])/http:\1/g' file
http://example.com    http://example.net http://www.flugov/individualfamily/prevention/index.html

答案 1 :(得分:0)

通过perl,

perl -pe 's~http(?!:)~http:~g' file

示例:

$ echo 'http://example.com    http//example.net http//www.flugov/individualfamily/prevention/index.html' | perl -pe 's~http(?!:)~http:~g'
http://example.com    http://example.net http://www.flugov/individualfamily/prevention/index.html

答案 2 :(得分:0)

或者,消耗任何和所有冒号:s/http:*/http:/g