Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing a string unless it contains a given substring with sed

I have a file which contains many links of the form <a href="foo">. These need to be given a .html extension, and I have the following command to do so:

sed -i 's/\(href="\)\([^"]*\)/\1\2.html/g' "$FILE"

However, I need to not do this on any link which has either a protocol (e.g. https:// or already has an extension (e.g. .css, .js—happy to match only these specifically.

How can I do this? Is sed still an appropriate tool here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This sed may work.

$ sed -Ei.bak '/https|\.[a-z]+/!s/"[^"]*/&.html/' input_file

If not, please consider adding more information to the question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading