mef51 mef51 - 9 months ago 26
HTML Question

Remove a specific tag and replace it with awk

I'm using a tool to generate some html that looks something like this:

<html>
<head>
<title>Blah</title>
<style>
/* stuff */
</style>
</head>


But I'd like a way to replace that style tag with some custom styling

<link rel="stylesheet" href="style.css">


possibly with
awk
or
sed
so that I can add it to my Makefile.
Is this possible?

Answer Source

awk to the rescue!

This is not xml/html aware but a basic text substitution...

$ awk '/<style>/   {f=1} 
       !f;
       /<\/style>/ {f=0; 
                    print "<link rel=\"stylesheet\" href=\"style.css\">"}' file

will give

<html>
<head>
<title>Blah</title>
<link rel="stylesheet" href="style.css">
</head>