Pattern matching spanning more than one line and substitution between two files - 
Author Message
 Pattern matching spanning more than one line and substitution between two files -

I am a perl newbie and this is what I am trying to do...very very new
at perl and trying to learn as fast as I can..

There are two text files say OldFile and NewFile
the contents are similar most of the time but some small changes occur
in the NewFile in some places. The values of the OLDFile are what I
want to keep along with any additions in the newFileFor example:

OldFile might have:
BLAH1 = 0
BLAH2 = 1
BLAH3 = 2
BLAH4 = 3

and NewFile might have:
BLAH1 = 0
BLAH2 = 5
BLAH3 = 6
BLAH4 = 3
BLAH5 = 7
BLAH8 = 9

I need to produce a third file, say ProductFile which will have

BLAH1 = 0
BLAH2 = 1
BLAH3 = 2
BLAH4 = 3
BLAH5 = 7
BLAH8 = 9

----
Also, the patten BLAH = <something> might stretch to more than one
line in the text file.
--------------------

Any suggestions on how i might do this - any short piece of code,
pseudo code, regexp for pattern matching and substitution spanning
more than one line [this is the one I am having the most problems
with] will be greatly appreciated!

Thanks for your time,
Vinod



Tue, 04 Jan 2005 21:05:31 GMT  
 Pattern matching spanning more than one line and substitution between two files -

Quote:

> I am a perl newbie and this is what I am trying to do...very very new
> at perl and trying to learn as fast as I can..

Learn to use the FAQs that were installed along with Perl.

Quote:
> Also, the patten BLAH = <something> might stretch to more than one
> line in the text file.
> regexp for pattern matching and substitution spanning
> more than one line [this is the one I am having the most problems

  ^^^^^^^^^^^^^^^^^^
  ^^^^^^^^^^^^^^^^^^

   perldoc -q 'more than one line'

      "I'm having trouble matching over more than one line.
       What's wrong?"

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 04 Jan 2005 22:25:27 GMT  
 Pattern matching spanning more than one line and substitution between two files -

Quote:

> I am a perl newbie and this is what I am trying to do...very very new
> at perl and trying to learn as fast as I can..

> There are two text files say OldFile and NewFile
> the contents are similar most of the time but some small changes occur
> in the NewFile in some places. The values of the OLDFile are what I
> want to keep along with any additions in the newFileFor example:

> OldFile might have:
> BLAH1 = 0
> BLAH2 = 1
> BLAH3 = 2
> BLAH4 = 3

> and NewFile might have:
> BLAH1 = 0
> BLAH2 = 5
> BLAH3 = 6
> BLAH4 = 3
> BLAH5 = 7
> BLAH8 = 9

> I need to produce a third file, say ProductFile which will have

> BLAH1 = 0
> BLAH2 = 1
> BLAH3 = 2
> BLAH4 = 3
> BLAH5 = 7
> BLAH8 = 9

> ----
> Also, the patten BLAH = <something> might stretch to more than one
> line in the text file.
> --------------------

> Any suggestions on how i might do this - any short piece of code,
> pseudo code, regexp for pattern matching and substitution spanning
> more than one line [this is the one I am having the most problems
> with] will be greatly appreciated!

Something like this should work:

#!/usr/bin/perl -w
use strict;

open OLD, 'OldFile' or die "Cannot open 'OldFile': $!";
my %old = split /(\s*=\s*\d+\n)/, do { local $/; <OLD> };

if ( open NEW, 'NewFile' ) {
    my %new = split /(\s*=\s*\d+\n)/, do { local $/; <NEW> };


    }

open FILE, '> ProductFile' or die "Cannot open 'ProductFile': $!";
print FILE %old;

__END__

John
--
use Perl;
program
fulfillment



Tue, 04 Jan 2005 23:08:45 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. combine two pattern matches to one match?

2. Using s// to Replace Text that Spans Two Lines

3. pattern matching across two lines

4. pattern matching across two lines

5. Matching pattern in more than one line !

6. reading lines from one file and pulling matching lines in another

7. Format a one line file to get two columns

8. pattern matching; see content file as one string

9. help - grep lines between two line patterns

10. Pattern matching across multiple lines in a file

11. pattern matching between multiple lines in a file

12. Pattern bug matching whitespace in multi-line match?

 

 
Powered by phpBB® Forum Software