简体   繁体   中英

How to inline CSS style file to HTML file with sed?

I'm trying to replace CSS file reference in HTML header with an inline style tag. I'm searching following line from the file.

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

and I want to replace the previous line with a line

<style>(content of style.css file here)</style>

I have following sed command for the operation

sed "s/<link rel=\\"stylesheet\\" href=\\"style.css\\" type=\\"text\\/css\\">/<style>$(cat style.css)<\\/style>/" index.html

However, sed does not like CSS file content in the command and gives an error.

sed: -e expression #1, char 84: unterminated `s' command

The command works OK without style.css file's content. How to make this command perform what I want to do or is there an alternative solution to perform the same operation?

Using GNU ;

sed $'/<link rel="stylesheet" href="style.css" type="text\\/css">/{e "<style>$(cat style.css)</style>"\n}' index.html

Try it online! (repl.it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM