Home > other >  How do I replace the first 69 lines of every .html file inside a folder with a 51 lines code?
How do I replace the first 69 lines of every .html file inside a folder with a 51 lines code?

Time:10-20

I have dozens of .html files all with different names of which old head and nav I need to replace with a new improved head and nav.

I know awk would be suitable for such operation but I am not confident on how to proceed.

NOTE: The title tag of the every html file is going to be replaced with something fixed, I think I will circumvent this issue by either leaving "Plant Database" or rendering the html title dynamically (every .html file has a correct file name), however that's also something I would need help on how to do. (document.title I assume?)

Old file content (first 69 lines):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/templatefrø.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Apotekerkattost - Malva sylvestris</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<link rel="shortcut icon" href="bilder/faviconefferus.ico">
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="header"><img src="bilder/efferus1.jpg" width="289" height="129" alt="Efferus veksthus" /></div>
<div id="menu">
<a href="index.html" ><img src="bilder/Home.jpg" border="0" width="128" height="35" /></a><a href="planteshop.html"><img src="bilder/Frørød.jpg" border=0 width="123" height="35" /></a>
<a href="soppshop.html"><img src="bilder/soppsort.jpg" border="0" width="138" height="35" /></a>
<a href="skogshop.html"><img src="bilder/skogshop.jpg" border="0" width="138" height="35" /></a>
<a href="Oppskrifter.html"><img src="bilder/Oppskrifter.jpg" border="0" width="118" height="35" /></a>

</div>
<br >
<div id="middle">
  <div id="submenu"><table width="201" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><div align="center"><img src="bilder/subtop.jpg" width="198" height="24" /></div></td>
  </tr>
  <tr>
    <td><div align="center" >
    
      <p>
      <a href="planteshop.html">Planteshop</a><br><br>
      <a href="fro.html">Frø- og plantekatalog</a><br><br>
            <a href="plantebistand.html">Plantebistand</a><br><br>
                  <a href="kurs.html">Kurskalender </a><br><br>
      <a href="bie.html">Humle- og bieplanter</a><br><br>
      <a href="hsoner.html">Planter inndelt etter herdighetssone</a><br><br>
      <a href="plantepakker.html">Plantepakker</a><br><br>
        <a href="trehage.html">Hva er en skoghage?</a><br><br>
         <a href="skoghagebilder.html">Bilder fra en skoghage</a><br><br>
        <a href="nitrogen.html">Nitrogenfiksering</a><br><br>
        <a href="jord.html">Jorddekkere</a><br><br>
        <a href="vind.html">Vindbrytere</a><br><br>
        <a href="tre.html">Trær</a><br><br>
        <a href="busker.html">Busker</a><br><br>
        <a href="perennial.html">Stauder</a><br><br> 
        <a href="systemplanter.html">Systemplanter</a><br><br> 
        <a href="andre.html">Andre planter</a><br><br>
           <a href="Rovinsekter.html">Skadeinsekter og nytteinsekter</a><br><br>
            <a href="plantesykdommer.html">Plantesykdommer</a><br><br>
        <a href="formering.html">Planteformering</a><br><br>
        <a href="beskjaering.html">Beskjæring</a><br><br>

        <a href="ugressmiddler.html">Ugressmiddel</a><br><br>
        <a href="http://www.facebook.com/EfferusVeksthus">Efferus veksthus på <img src="bilder/fb.jpg" width="15" height="15" /></a><br><br>
            <a href="https://www.facebook.com/groups/662923897182715/?fref=ts">"Dyrking av Storhassel i Norge" på <img src="bilder/fb.jpg" width="15" height="15" /></a><br><br>
             <a href="https://www.facebook.com/groups/245491145952851/?fref=ts">Skadeinsekter,  nytteinsekter og plantesykdommer på <img src="bilder/fb.jpg" width="15" height="15" /></a><br><br>
        <a href="hvemerfro.html">Hvem er Efferus?</a><br><br>
        <a href="kontaktfro.html">Kontakt</a><br><br>
        <a href="open.html">Åpningstider</a><br><br>
      </p>
    </div></td>
  </tr>
  <tr>
    <td><div align="center"><img src="bilder/subbottom.jpg" width="198" height="35" /></div></td>
  </tr>
</table>
</div>

New file content (51 lines):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Plant Database</title>
    <link rel="shortcut icon" href="bilder/deciduous-tree.ico">
    <link href="main_styles.css" rel="stylesheet" type="text/css"/>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Domine:wght@400;600;700&display=swap');
        @import url('https://fonts.googleapis.com/css2?family=Domine:wght@400;600;700&family=Lato:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap');
        @import url('https://fonts.googleapis.com/css2?family=Playfair Display:ital,wght@0,400;0,500;1,400&display=swap');
    </style>
</head>
    
<!-- ALWAYS ON HOME BUTTON -->
<a href="/NEW_index.html"><img id=left_space style="position: fixed; top: 1.5rem;" src="./bilder/Efferus_logo_black.png" width="100px"></a>

<!-- HEADER -->
<nav  >
    
    <!-- LOGO -->
    <div id=left_space ><a href="/NEW_index.html"><img src="bilder/Efferus_logo_white.PNG" width="100px" alt="efferus_logo"/></a></div>
    
    <!-- NAVIGATION MENU -->
    <ul >
      
        <!-- USING CHECKBOX HACK -->
      <input type="checkbox" id="checkbox_toggle" />
      <label for="checkbox_toggle" >&#9776;</label>
      
      <!-- NAVIGATION MENUS -->
      <div >
        
        <li><a href="./plantebistand.html">Plantebistand</a></li>
        <li >
          <a href="./database.html">Database</a>
          <!-- <ul >
            <li><a href="./fro.html">Frø og Planter </a></li>
            <li><a href="./Oppskrifter.html">Konserving</a></li>
            <li><a href="./husapotek.html">Husapotek</a></li>
            <li><a href="/">Dropdown 3</a></li>
            <li><a href="/">Dropdown 4</a></li>
          </ul> -->
        </li>
        <li><a href="./hvemer.html">Hvem er Efferus?</a></li>
        <li><a href="./kontakt.html">Kontakt</a></li>
      </div>
    </ul>
</nav>

CodePudding user response:

You can do it with head and tail - for example, I am replacing first 3 lines of f1.txt with the first 2 lines from f2.txt

>>> cat f1.txt
a
b
c
d
e

>>> cat f2.txt
x
y
z
a
b

Output

>>> head -n 2 f2.txt;tail -n  4 f1.txt
x
y
d
e

CodePudding user response:

Here's a way with awk and a quoted heredoc, keeping the old title:

awk -v head_size=69 '
    FNR == 1 {
          fn
        close (outfile)
        outfile = FILENAME ".new"
        ok = 0
    }
    fn == 1 {
        head = head $0 "\n"
        next
    }
    fn == 2 && FNR == 1 {
        if ( match(head,/<title>.*<\/title>/) ) {
            head_part1 = substr(head, 1, RSTART-1)
            head_part2 = substr(head, RSTART RLENGTH)
        } else {
            print "error: title not found in replacement" | "cat 1>&2"
            exit 1
        }
    }
    FNR <= head_size && match($0,/<title>.*<\/title>/) {
        title = substr($0,RSTART,RLENGTH)
        ok = 1
    }
    FNR == head_size 1 {
        if ( ok )
            printf("%s%s%s", head_part1, title, head_part2) > outfile
        else
            print "warning: skipping " FILENAME ": title not found" | "cat 1>&2"
    }
    FNR > head_size && ok { print > outfile }
' - *.html <<'EOF'
<!DOCTYPE html>
<html lang="en">
...
EOF

for f in *.html.new
do
    mv -v "$f" "${f%.new}"
done
  • Related