ryan(ma) has asked for the wisdom of the Perl Monks concerning the following question:

Hello Brother,

Perhaps you can help me. I'm trying to format a book for display on an e-Reader. The original is html. In the original there are footnotes in the text. The footnote number in the main body points to the footnote text through a hyperlink. The text of the footnote is in a different file.

What I'd like to do is this:

  1. Search all the files for the first footnote text and store the match as a variable.
  2. Then delete the footnote text.
  3. Search all the files for the corresponding footnote number in the main body and insert the footnote text there as part of the main body.
  4. Then repeat the action for all the footnotes throughout every file in the directory.

To give an example:

The main body text looks like this:

some words<a href="different_file.html#note1">1</a>

The footnote text looks like this:

<a id="note1">Here is the footnote text.</a>

I can write a regular expression to pull out the footnote text from the above string. And I can write another regexp to insert that text in the appropriate place in the main body. But I don't know Perl. There are hundreds of footnotes and it would be great to automate the process.

The reason I want the footnote text in the main body is because I'm using Latex to create the book, and that's the best way to reference footnotes in that program.

I hope this makes sense. If there's a convenient way to do this, I'd appreciate any help.

Thanks! best wishes, Ryan

Replies are listed 'Best First'.
Re: multiple search/replace across multiple files
by moritz (Cardinal) on Jun 06, 2011 at 14:47 UTC
    I can write a regular expression to pull out the footnote text from the above string. And I can write another regexp to insert that text in the appropriate place in the main body. But I don't know Perl.

    Then this is the perfect occasion for learning Perl. Start with perlintro.

    If you need any specific advice because you don't understand something from that tutorial, feel free to ask.

    But we are not a code writing service, so you'll have to show some effort on your own.

Re: multiple search/replace across multiple files
by kennethk (Abbot) on Jun 06, 2011 at 20:36 UTC
    As you wish to manipulate HTML, you may find the HTML::Tree module useful.