Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:27:02+00:00 2026-05-30T05:27:02+00:00

I’ve been tasked with programmatically splitting up an HTML master template into subtemplates, performing

  • 0

I’ve been tasked with programmatically splitting up an HTML master template into subtemplates, performing some minor modifications to each template, and saving the resultant files.

Why I’m using Bash (you can skip this to get to the question)

(edited) The choice to use Bash is fairly arbitrary. I know it runs Red Hat 5.5, so I’m writing a Bash script. It does have a PHP interpreter, but I decided not to use PHP for this, since the same server will be hosting the site which uses PHP and will probably see a lot of traffic, so I’m afraid of tying up a FastCGI socket every hour doing this operation (I don’t control the frequency of script execution, just what is executed). I can also install whatever interpreter I want (script languages I actually know already: Perl, Python, PHP, maybe Lua). That, however, is a different question. This question assumes I want to use a Bash script.

The Problem

I have a master template file, which looks something like:

<!DOCTYPE html PUBLIC .... >
<html lang="en" ...>
<head> ... </head>
<body>
    <div id=...></div>
    <div id=...></div>
</body>
</html>

From this, I need to parse from the top of the document up to </head>, strip a few lines from that section and add one in, replace the <title> placeholder with the actual title, and save that to a file. Then I need to parse the <body> and first <div> out as a separate file, and then finally the second <div> (to which I also need to make some changes in the page footer). I will be discarding </body> and </html> since this template is actually part of a two-layer template (the replaced page title will use a Smarty variable to get its text).

The Conundrum

The question is, is there an easier/better way to do this than regex? I know Bash provides the compound command [[ htmlstring =~ "/regex/" ]] and ${BASH_REMATCH} to match or replace, but I also know that parsing HTML with regex is generally a bad idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:27:03+00:00Added an answer on May 30, 2026 at 5:27 am

    If the HTML file you are parsing has a known fixed structure, you can use awk for this. It’s not very hard to write a program that keeps state in a variable (e.g., waiting for header, parsing header, waiting for body) and do different things as you read the file. It also supports regular expressions and you can put everything into a well-structured file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.