On Wed, May 30, 2007 at 07:21:49AM -0400, Andrew Brown wrote:
> I need to merge two files, a text file and a note file.
> 
> The text file contains note calls thus
> 
>       Foo{124} bar{125}
> 
> but they could be coded otherwise to suit.
> 
> The notes file contains one note per line
> 
>       124. Another note.
>       125. A longer note.
> 
> and could be coded otherwise to suit.
> 
> What I'd need to achieve is one merged file, something like:
> 
>       Foo{124-Another note.} bar{125-A longer note.}
> 
> The files are several thousand lines long and contain around 750 note  
> calls or notes.
> 
> Any suggestions warmly welcomed !

A good approach is to read the notes file, building a table of note ids to
notes, then read the text file, substituting in the notes.

Here's a Perl script that does it.  I wasn't sure what you wanted to do
with missing notes or repeated notes, so I just had it die in those cases.

You can run it from the command line, and redirect the output to a new
file.


#!perl

# interpolate notes into a text file
# usage: mynotes.pl <text file> <notes file>
# 
# text file contains note calls: Look at this note{123}
# notes file contains notes, one per line: 123. Here is a note.
# 
# output is to stdout: Look at this note{123-Here is a note.}

use warnings;
use strict;

my $text_file = shift
  or die "Must specify text file.\n";
my $notes_file = shift
  or die "Must specify notes file.\n";

open(TEXT, $text_file)
  or die "Can't open '$text_file': $!\n";
open(NOTES, $notes_file)
  or die "Can't open '$notes_file': $!\n";

my %notes;

while (<NOTES>) {
  chomp;

  my($id, $note) = /^(\d+)\.\s*(.*)\z/
    or die "Invalid entry in '$notes_file', line $..\n";

  if (exists $notes{$id}) {
    die "Conflicting entry for note $id in '$notes_file', line $..\n";
  }

  $notes{$id} = $note;
}

while (<TEXT>) {
  s<\{(\d+)\}>
   {
    my $id = $1;
    $notes{$id} or die "Unknown note $id in '$text_file', line $..\n";
    "{$id-$notes{$id}}";
   }ge;

  print;
}

__END__


Ronald

-- 
------------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_script.shtml>
List archives: <http://www.listsearch.com/bbeditscripting.lasso>
To unsubscribe, send mail to:  <[EMAIL PROTECTED]>

Reply via email to