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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Help for generating multiple file from singl e file and parsing it.
  • Download Code

Replies are listed 'Best First'.
Re: Help for generating multiple file from singl e file and parsing it.
by Marshall (Canon) on Oct 15, 2011 at 19:47 UTC
    update:
    #!usr/bin/perl -w use strict; use Data::Dump qw(pp); my %data; foreach (<DATA>) { next if (/^\s*$/); #skip blank lines my ($str, $date) = m/^#RD(.+)?#OE.+?(\d+)/; push @{$data{"$str"."txt"}},"OE#$date"; } print pp \%data; =Prints { "#,3004,,,,,N,MANS.20111011.2,txt" => ["OE#284110217106"], "#,3023,,,,,N,MANS.20111011.2,txt" => ["OE#284110161544", "OE#284110 +161544"], } =cut __DATA__ #RD#,3023,,,,,N,MANS.20111011.2,#OE#,CR,N,284110161544,,Y,MANS,2011101 +1064839000,20111011~1M9LQV49HM #RD#,3023,,,,,N,MANS.20111011.2,#OE#,CR,N,284110161544,,Y,MANS,2011101 +1064839000,20111011~1M9LQV49HM,20111011064925000,20111011~1M9LQV49Ho, +,,20111011064925000 #RD#,3004,,,,,N,MANS.20111011.2,#OE#,RT,N,284110217106,,Y,MANS,2011101 +1065244000,20111011~1J6Q7ETYXH,,20111011~1J6Q7ETYXH,NVMI,201110110652 +44000,2324,E
      thank you very for you reply.
      while ia m running this code it is giving me some error which is
      Use of uninitialized value $str in string at test2.pl line 13, <DATA> line 8968. Use of uninitialized value $date in concatenation (.) or string at test2.pl line 13, <DATA> line 8968. Use of uninitialized value $str in string at test2.pl line 13, <DATA> line 8968. Use of uninitialized value $date in concatenation (.) or string at test2.pl line
      i am not able to understand.
        what you describe would be a symptom of say a blank line at the end of the DATA section although I'm not sure how you get to line 8968! The code should skip blank lines and this shouldn't matter.

        I just ran the code here again. Download and run it verbatim.

Re: Help for generating multiple file from singl e file and parsing it.
by pvaldes (Chaplain) on Oct 15, 2011 at 18:56 UTC
    I need to pick data after #RD# till #OE#
    open my $INFILE, '<', 'myfile'; my $number = 0; while ($INFILE){ $_ =~ m/\#RD\#(.*?)\#OE\#/; my $number++; open my $OUTFILE, '>', "outfile$number"; print $OUTFILE $1; close $OUTFILE; } close $INFILE;
    date after #OE# till the end of line will be inside individual output file.if data between #RD# and #OE# is repeated that will come in same individual output file.

    ??. don't understand this. Could you explain better what do you want to do?

      thank you very much for your kind reply.
      actually i want to pick data between #RO# and #OE# use this data as name of output file for example in give data individual file generated will be
      1. "3023 N MANS.20111011.2.txt"
      2. "3004 N MANS.20111011.2.txt" inside this newly generated file i want to write data after #OE# till newline like in file eg. "3023 N MANS.20111011.2.txt" data should written "#OE#,CR,N,284110161544,,Y,MANS,20111011064839000,20111011~1M9LQV49HM"

      one more thing which i need to do is if output file having same name like
      "3023 N MANS.20111011.2.txt"
      3023 N MANS.20111011.2.txt
      the data come inside single file. appreciate for you help.
        You can create a filename with "," and spaces inside, but I don't recommend this. You should replace all "," in the first chain (i.e by "_") before passing this as a filename.
Re: Help for generating multiple file from singl e file and parsing it.
by locked_user sundialsvc4 (Abbot) on Oct 16, 2011 at 13:51 UTC

    A more formally defined parser system such as Parse::RecDescent might be useful here.

    In essence, systems like these let you define a grammar which stipulates the valid constructs in the file, and what is to be done as each construct is recognized.   The parser is constructed and executed on-the-fly by code that is known to be highly reliable.