Thursday, April 21, 2011

simple SED command

I have a html report that comes out of a program we use. Its extremely plain html and id like to work on fixing it up just a bit.

What code would I need to do in sed to replace

<html>  with

<html><link rel="stylesheet" type="text/css" href="LivingInStyle.css">

Thanks, Russ

From stackoverflow
  • sed 's/<html>/<html><link rel="stylesheet" type="text\/css" href="LivingInStyle.css">/g' file.html
    

    That will output the new file to the console and you can redirect it wherever you need. Or you can use -i to edit inplace.

    EDIT: Forgot to escape the slash

    ephemient : I'd choose a different delimiter, maybe 's!...!...!g', to avoid having to escape. But that's personal opinion.
    Stephan : @ephemient: That's probably a better choice, but I feel odd using anything but slash
  • You can also use:

    sed -e 's!<html>!<html><link rel="stylesheet" type="text/css" href="LivingInStyle.css">!' < file
    

    i.e. use another separator instead of escaping the slash.

0 comments:

Post a Comment