1

My directory ~/foo contains many HTML files. Each one has a different unwanted title element. That is, each file contains the code

<title>something unwanted</title>

Many of these files also contain some span elements like this

<span class="org-document-info-keyword">#+Title:</span> 
<span class="org-document-title">correct title</span>

I'd like to write a script that scans each HTML file and, for each file that contains a code-block of the second type, replaces the unwanted title with the correct title.

Once the title has been replaced, I'd like the script to remove the code in the second block.

For example, running the script on

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!-- Created by htmlize-1.47 in css mode. -->
<html>
  <head>
    <title>foo.org</title>
    <style type="text/css">
    <!--
      body {
        color: #839496;
        background-color: #002b36;
      }
      .org-document-info {
        /* org-document-info */
        color: #839496;
      }
      .org-document-info-keyword {
        /* org-document-info-keyword */
        color: #586e75;
      }
      .org-document-title {
        /* org-document-title */
        color: #93a1a1;
        font-size: 130%;
        font-weight: bold;
      }
      .org-level-1 {
        /* org-level-1 */
        color: #cb4b16;
        font-size: 130%;
      }

      a {
        color: inherit;
        background-color: inherit;
        font: inherit;
        text-decoration: inherit;
      }
      a:hover {
        text-decoration: underline;
      }
    -->
    </style>
  </head>
  <body>
    <pre>
<span class="org-document-info-keyword">#+Title:</span> <span class="org-document-title">my desired title
</span><span class="org-document-info-keyword">#+Date:</span> <span class="org-document-info">&lt;2015-08-23 Sun&gt;
</span>
<span class="org-level-1">* hello world</span>

Vivamus id enim.  

</pre>
  </body>
</html>

should result in

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!-- Created by htmlize-1.47 in css mode. -->
<html>
  <head>
    <title>my desired title</title>
    <style type="text/css">
      <!--
      body {
          color: #839496;
          background-color: #002b36;
      }
      .org-document-info {
          /* org-document-info */
          color: #839496;
      }
      .org-document-info-keyword {
          /* org-document-info-keyword */
          color: #586e75;
      }
      .org-document-title {
          /* org-document-title */
          color: #93a1a1;
          font-size: 130%;
          font-weight: bold;
      }
      .org-level-1 {
          /* org-level-1 */
          color: #cb4b16;
          font-size: 130%;
      }

      a {
          color: inherit;
          background-color: inherit;
          font: inherit;
          text-decoration: inherit;
      }
      a:hover {
          text-decoration: underline;
      }
    -->
    </style>
  </head>
  <body>
    <pre>
      <span class="org-document-info-keyword">#+Date:</span> <span class="org-document-info">&lt;2015-08-23 Sun&gt;
      </span>
      <span class="org-level-1">* hello world</span>

      Vivamus id enim.  

    </pre>
  </body>
</html>

Is there a tool in linux that can easily do this?

2
  • take a look at sed command.
    – BitsOfNix
    Commented Aug 24, 2015 at 7:26
  • @BitsOfNix I'm aware of the sed command but I'm not sure how to use it to accomplish this specific task. Could you elaborate? Commented Aug 24, 2015 at 7:57

1 Answer 1

2

You are probably best off scripting something. This script is not robust (doesn't check for empty strings, doesn't account for desired title being on several lines etc) but it might be something to get you started. Backup before you start doing anything crazy.

#! /bin/bash

FILES="./*.html"
for f in $FILES
do
     grep '.*org-document-title">.*' $f |\
         sed -e 's/.*org-document-title">\([^<]\+\).*/\n\1/g' |\
         tail -n 1 |\
         xargs -I new_title sed -i.bak 's/<title>[^>]\+<\/title>/<title>new_title<\/title>/g' $f
done

This only replaces the title with the new my desired title. You could expand by doing another pass and getting rid of the unwanted span elements.

2
  • This is really great! I'm still having trouble writing s script to remove the unwanted span elements. I think the problem is that sometimes the elements span multiple lines. Any suggestions? Commented Aug 24, 2015 at 18:36
  • This is probably why you are going to need a more robust tool set. You could try out something with Python's beautiful soup, as shown in this stack overflow question: stackoverflow.com/questions/11808776/…. There are probably some good Perl solutions, but I am not very up to par in my Perl game. Commented Aug 24, 2015 at 20:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .