In the file header.php on the next line after the tag you need to add a few lines, for example

<link rel="dns-prefetch" href="//fonts.googleapis.com"> <link rel="dns-prefetch" href="//themes.googleusercontent.com"> 

If you add 1 line - everything is OK, but it is worth adding one more

 sed '/<head>/ a \ <link rel="dns-prefetch" href="//fonts.googleapis.com"> <link rel="dns-prefetch" href="//themes.googleusercontent.com"> ' header.php > header2.php 

sed: -e expression # 1, symbol 70: unknown command: `<'

And is it possible to do without creating an output file?

  • the carriage transfer must be written as \n , the -i sed parameter causes it to rewrite the source file - Mike
  • I tried different combinations - it does not work. sed: -e expression # 1, symbol 72: unknown command: `<' - Magi
  • If I understood the problem correctly, then after href="//fonts.googleapis.com"> I need to add a backslash "\". - zombic
  • This problem is simply not solved in the bash programming language. You need an XML parser. Use python, for example. There is a great lxml module (ElementTree). - 0andriy

2 answers 2

Good day. If you understand the problem correctly, you can solve it like this:

 sed -i '/<head>/ a \<link rel=\"dns-prefetch\" href=\"\/\/fonts.googleapis.com\"\>\n\<link rel=\"dns-prefetch\" href=\"\/\/themes.googleusercontent.com\"\>' header.php 

    With your approach, it is obvious that there will be errors, since there is no symbol renova in the shell. Your example needs to be redone with:

     sed '/<head>/ a \ <link rel="dns-prefetch" href="//fonts.googleapis.com"> <link rel="dns-prefetch" href="//themes.googleusercontent.com"> ' header.php > header2.php 

    on

     sed -i '/<head>/ a \ <link rel="dns-prefetch" href="//fonts.googleapis.com"> \ <link rel="dns-prefetch" href="//themes.googleusercontent.com"> \ ' header.php 

    In this case, the intermediate file will not be created, the file will be immediately overwritten.