After examining the sample input and output files that Joes emailed to me, as well as some clarified rules, I think that I was actually quite close with my original script. Here's the final final (see below) version:
#!/usr/local/bin/perl -w use strict; $/ = "\n0"; # read in one record at a time $^I = '.bak'; # modify input files in place, save originals with .bak my %convert = ( HEAL => 'Medical', HIST => 'Biography', EDUC => 'Educated', RESI => 'Resided', OCCU => 'Occupation', ); my $convert_re = join '|', keys %convert; $convert_re = qr/\b($convert_re)\b/; while (<>) { s/$convert_re/NOTE $convert{$1}:/g; if (/^.*SOUR/) { s/^1 NAME/1 TITL/m; s/^1 NAME/1 TEXT/m; s/^1 NAME/2 CONT/mg; } } continue { print; }
I made two changes. The input record separator is now "\n0", because each section begins with a line starting with 0. (Although this will have each "\n0" read in as the end of the previous record, that doesn't make a difference for our purposes.) In the substitutions for NAME, I added a beginning of line anchor and removed the word 'data' (turns out that bit was just a placeholder for actual data). Also I fixed my typo of TITLE to be TITL (everything is four letters in this format).

Update: Make that four changes. The change of NAME to TITL is only supposed to occur for sections that start 0 @Snnn@ SOUR, not for all sections. And I stupidly forgot the /m modifier on those regexes when I added the anchors. Thanks for the notice of the problems, tachyon! (Notwithstanding the suggestion that I'm reading in one line at a time; I'm actually reading in a block of lines ending with a newline followed by a zero, as I intended.)


In reply to Re: Converting GEDCOM files by chipmunk
in thread Converting GEDCOM files by Joes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.