in reply to Re: pattern matching between two specific strings
in thread pattern matching between two specific strings

I could not get the code you provided to work
I've listed a portion of the file I'm working from which contains numerous datasets having the following format, see below: I need to extract the data between Final graph set matrix and PLUTO4 finished each time it is present in the file and seperated by a title of some sort.

hope you can help, cheers
pattern:<br> /([A-Z]\s\d+,\s\d+\(\s*\d+\))/g
FILE CONTAINS DATA IN THE FOLLOWING FORMAT

Final graph set matrix
----------
C 1, 1( 9)
----------
C 2, 2(11) C 1, 1( 4)
----------
C 2, 2(18) C 1, 2(11) C 1, 1( 9)
R 2, 2( 8)
----------
C 1, 2(11) C 2, 2(18) C 2, 2(11) C 1, 1( 4)
R 2, 2(18)
PLUTO4 finished


Final graph set matrix
----------
C 1, 1( 4)
----------
C 2, 2(18) C 1, 1( 4)
R 2, 2(18)
PLUTO4 finished


Final graph set matrix
----------
C 1, 1( 4)
----------
C 2, 2(18) C 1, 1( 4)
R 2, 2(18)
----------
C 2, 2(11) C 2, 2(11) C 1, 1( 9)
----------
C 2, 2(11) C 2, 2(11) C 2, 2(18) C 1, 1( 9)
R 2, 2( 4)
PLUTO4 finished

Replies are listed 'Best First'.
Re: Re: Re: pattern matching between two specific strings
by zby (Vicar) on Jul 14, 2003 at 11:36 UTC
    Please post the code you derived from mine - I'll see what is the problem with it.
      This is how I used your origenal code.

      cheers harry
      #!/usr/local/bin/perl $in_filename = "graph_set.out"; open (IN,"$in_filename") or die "Can't open $in_filename:$!\n"; my $firsttime = 1; while(<IN>){ if(/Final graph set matrix/ .. /PLUTO4 finished/){ if($firstime--){ print "\nnew data set\n"; } my @gset_match = ($string =~ ([A-Z]\s\d+,\s\d+\(\s*\d+\))/g); open (TEXT, ">>graph_set.txt") or die "Can't create graph_set.txt: + $!\n"; foreach $_(@gset_match) { print TEXT "$_\n"; } close TEXT; } } close (IN);
        There was an obvious mistake - this should be better (but I did not tested it - I leave that task to you):
        #!/usr/local/bin/perl $in_filename = "graph_set.out"; open (IN,"$in_filename") or die "Can't open $in_filename:$!\n"; my $dsno; while(<IN>){ if(/Final graph set matrix/){ print "\n", $dsno++, " data set\n"; } if(/Final graph set matrix/ .. /PLUTO4 finished/){ my @gset_match = ($string =~ /([A-Z]\s\d+,\s\d+\(\s*\d+\))/g); open (TEXT, ">>graph_set.txt") or die "Can't create graph_set.txt: + $!\n"; foreach $_(@gset_match) { print TEXT "$_\n"; } close TEXT; } } close (IN);
        Update: Added a / at line 12.