cesar andavisa
cesar andavisa

Reputation: 340

find and replace with sed command and regexp

hi i'm trying to find and replace the link rel tags into a file with this command :

sed 's/(<link\b.+href=\")(?!http)([^\"]*)(\".*>)/someText/g'  www/file.html

but it doesn't work , could somebody help me please ? thank one example that i want to do is :

replace all this links tags :

 <!-- Common app CSS -->
<link rel="stylesheet" type="text/css" href="less/med.ui.css" />
<link rel="stylesheet" type="text/css" href="less/timeline.css" />
<link rel="stylesheet" type="text/css" href="less/animate.min.css" />
<link rel="stylesheet" type="text/css" href="less/magic.min.css" />

<!-- Common UI CSS -->
<link rel="stylesheet" type="text/css" href="js/app/common/less/common.css" />
<link rel="stylesheet" type="text/css" href="less/sidebar.css" />

<!-- Specific App CSS -->
<link rel="stylesheet" type="text/css" href="js/app/timeline/less/timeline.css" />

with only this line :

<link rel="stylesheet" type="text/css" href="js/app/globa/less/global.css" />

Upvotes: 0

Views: 225

Answers (3)

cesar andavisa
cesar andavisa

Reputation: 340

this is the final solution :

perl -i -0777pe 's/(<link\b.+href=\")(?!http)([^\"]*)(\".*?>)/<link rel=\"stylesheet\" type=\"text\/css\" href=\"www\/less\/global_bst.css\" \/>/gs' www/*.html

i hope that this code will be helpful to you too. thanks a lot for the answers .

Upvotes: 0

repzero
repzero

Reputation: 8412

doing this with sed

sed -i backup.bak '/<link\b.\+href=\"/{/href="[^"]*http/!{s#\(<link .\+href="\).*#\1js/app/globa/less/global.css" />#}}' www/file.html

EDIT:

or

sed -i backup.bak '/<link\b.\+href=\"/{
/href="[^"]*http/!{
s#\(<link .\+href="\).*#\1js/app/globa/less/global.css" />#
}
}'

backup.bak is a backup of the original file with a .bak extension

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Since sed won't support lookarounds, i suggest you to use Perl instead.

perl -pe 's~(<link\b.+href=\")(?!http)([^\"]*)(\".*>)~\1js/app/globa/less/global.css" />~g' www/file.html

To save the changes made.

perl -i -pe 's~(<link\b.+href=\")(?!http)([^\"]*)(\".*>)~\1js/app/globa/less/global.css" />~g' www/file.html

Upvotes: 1

Related Questions