siffland has asked for the wisdom of the Perl Monks concerning the following question:

I have a document in the following format

=====EDO=FED00123===== XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX =====EDO=FED00124===== XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX

There can be any number of FED00XXX segments and all the X's can be different depending on the report information (there can be a varying amount of XXXXX lines form 5 to 50). I have comments i need to paste after the "UNIX Page: XX" line. I am pulling them from a sqlite database and the comments correspond with the FED00XXX number.

=====EDO=FED00123===== XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX Comment FED00123 =====EDO=FED00124===== XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX Comment FED00124

The database part is easy, i just have no idea how to search for the FED number and insert the comments (will be a variable in the script pulled from the database) after the "UNIX Page: XX" line following the FED number. The FED number is pulled from the database as variable $FED and the comment as $COMMENT.

Any help would be appreciated, if anything needs to be made more clear ask, i just did not know how to explain the problem.

Thanks Sean

Replies are listed 'Best First'.
Re: Paste comments in a document
by BrowserUk (Patriarch) on Apr 06, 2010 at 19:42 UTC

    A variation.

    #! perl -sw use strict; my %comments = ( FED00123 => 'Comment FED00123', FED00124 => 'Comment FED00124', ); $/ = 'UNIX'; ## Read the bulk as a block while( <DATA> ) { my( $id ) = m[(FED.....)]; print; { local $/ = "\n"; print scalar <DATA>; } ## tidy up print "\n$comments{ $id }\n"; } __DATA__ ...

    Gives:

    C:\test>junk48 =====EDO=FED00123===== XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX Comment FED00123 =====EDO=FED00124===== XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX Comment FED00124

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Paste comments in a document
by ikegami (Patriarch) on Apr 06, 2010 at 19:34 UTC
    my $id; while (<>) { if (/^====EDO=(\w+)=====$/) { $id = $1; } print; if (/^UNIX Page:/ && defined($id)) { print("\nComment ", fetch_comment($id), "\n"); } }
    where fetch_comment fetches the appropriate comment from the database.
Re: Paste comments in a document
by Anonymous Monk on Apr 06, 2010 at 19:32 UTC
    Like this?
    #!/usr/bin/perl my $EDO; while (<DATA>) { ($EDO) = $_ =~ /=*EDO=(\w+)=*/ if /^=/; print; print "\nComment $EDO\n" if ( /^UNIX/ ); } __DATA__ =====EDO=FED00123===== XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX =====EDO=FED00124===== XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX UNIX Page: XX

      This worked great and i was able to tag the results from the database, although i hit one snag one of the results is 2009-A-2341 (instead of FEDXXXXX, those are the only 2 formats i checked). With the following code i can make it work.

      ($EDO) = $_ =~ /=*EDO=(\w+[-]\w+[-]\w+)=*/ if /^=/;

      But i need:

      ($EDO) = $_ =~ /=*EDO=(\w+)=*/ if /^=/;
      To make it all work. Is there a quick way to make it match anything between the EDO= and the next = sign. Thanks again you guys are the best. Sean

        make it match anything between the EDO= and the next = sign
        ($EDO) = /=EDO=([^=]+)=/ if /^=/;

        (the character class [^=] matches anything but a = sign)

Re: Paste comments in a document
by siffland (Novice) on Apr 07, 2010 at 21:17 UTC

    Here is the finished(i use that term lightly as it is never really finished) script. I dont know if people usually post them when they are finished but in case someone else searches on this thread. Thanks again everyone.

    #!/usr/bin/perl use strict; #use warnings; use DBI; my $all = (); my $EDO = (); my $comment = (); my $now = localtime(); my $result = (); my $dbargs = {AutoCommit => 0, PrintError => 1}; open (DATA, "@ARGV"); my $dbh = DBI->connect("dbi:SQLite:dbname=scan.db","","",$dbargs); while (<DATA>) { ($EDO) = /=EDO=([^=]+) Result=/ if /^=/; $all = $dbh->prepare("select comment FROM rr where EDO=\'$EDO\'"); $all->execute(); my $result = $all->fetchrow_hashref(); print; print "\nMitigation: $result->{comment}\n\n" if ( /^UNIX Checklis +t/ ); } close (DATA); $dbh->commit(); $dbh->disconnect(); __DATA__
    Sean

Re: Paste comments in a document
by 7stud (Deacon) on Apr 06, 2010 at 20:03 UTC
    Too late.