Shell script to replace a variable in a HTML document Shell script to replace a variable in a HTML document shell shell

Shell script to replace a variable in a HTML document


Using awk

[jaypal:~/Temp] cat html.sh #!/bin/bashauthor="Github INC."name="Github"description="social coding"awk '{sub(/\$name/,name);sub(/\$author/,author);sub(/\$description/,description);}1' name="$name" author="$author" description="$description" inputfile

Using sed

[jaypal:~/Temp] cat html1.sh #!/bin/bashauthor="Github INC."name="Github"description="social coding"sed -e '/\$name/s//"$name"/' -e '/\$description/s//"$description"/' -e '/\$author/s//"$author"/' inputfile


see the test (with awk) below: actually sed should work as well.

kent$  cat main.html <!DOCTYPE html> <html> <head>     <title>$name</title>     <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />     <meta name="robots" content="index, follow" />     <meta name="author" content="$author" />     <meta name="description" content="$description" />     <link rel="shortcut icon" href="favicon.png" />     <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body>kent$  cat doIt.sh #!/bin/bashauthor="Github INC."name="Github"description="social coding"awk -vauthor="$author" -vname="$name" -vdesc="$description" '{gsub(/\$name/,name);gsub(/\$author/,author);gsub(/\$description/,desc)}1' main.htmlkent$  ./doIt.sh  <!DOCTYPE html> <html> <head>     <title>Github</title>     <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />     <meta name="robots" content="index, follow" />     <meta name="author" content="Github INC." />     <meta name="description" content="social coding" />     <link rel="shortcut icon" href="favicon.png" />     <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body>


vim:

:%s/\v"\zs\$\w+\ze"/\={'$author':'Github INC.', '$name':'Github', '$description':'social coding'}[submatch(0)]/g