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

Hi, I need a tool or command which can take a file as input, take two strings (one will be start string and other will be end string), copy all the contents between the string (along with start and end string) to new file output. I need this as soon as possible. I am trying to write code but not able to get proper logic. Any input is helpful. The starting and ending strings will be like #BG and #EN and this may vary from file to file.

Replies are listed 'Best First'.
Re: File Filtering... Help
by hardburn (Abbot) on Sep 12, 2003 at 16:54 UTC

    Text::Balanced should do what you need.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: File Filtering... Help
by shenme (Priest) on Sep 12, 2003 at 19:45 UTC
    Shocked I am, shocked!   No one remembered '..' ?
    while( <DATA> ) { if( m/#BG/..m/#EN/ ) { print "$.: $_"; } } __DATA__ blah blah blah #BG blah blah blah blah blah blah #EN blah blah blah blah #BG blah #EN blah blah blah blah
    will output
    2: #BG blah blah blah
    3:  blah blah blah
    4: #EN
    6:  blah #BG blah #EN blah
    
    and as a one-liner Perl
    perl -ne "print if m/#BG/..m/#EN/;" anon03.dat
    
Re: File Filtering... Help
by BrowserUk (Patriarch) on Sep 12, 2003 at 18:09 UTC

    If your files aren't too big, then this might get you started.

    perl -0777 -e"print <stdin> =~ m[( \Q$ARGV[0]\E .*? \Q$ARGV[1]\E )]sxg +" "#BG" "#EN" < yourinputfile >youroutputfile

    Note: The quoting will be different for *nix. Using 's instead of "s will probably work, and you may not need to quote your start and end strings depending upon what they contain and which shell you are using.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: File Filtering... Help
by hmerrill (Friar) on Sep 12, 2003 at 17:13 UTC
    #!/usr/bin/perl -w my $file = shift; my $start = shift; my $end = shift; my $outfile = "$file.out"; open(IN, "<$file") || die "Can't open $file!\n"; open(OUT, ">$outfile") || die "Can't open $outfile!\n"; while(<IN>) { if (/$start(.*)$end/) { print OUT $1; } }
    With your less-than-ample description, that's the best I can do in short order. Note that this code is completely *untested*, but shouldn't need too much messaging.

    HTH.
      <massage> #!/usr/bin/perl -w use strict; my $file = shift; #always! my $start = quotemeta shift; #quote meta chars my $end = quotemeta shift; #quote meta chars my $outfile = "$file.out"; open(IN, "<$file") || die "Can't open $file!\n"; open(OUT, ">$outfile") || die "Can't open $outfile!\n"; my $file_contents = do {local $/; <IN>}; #slurp! while ($file_contents =~ /($start.*?$end)/gs) { print OUT $1, "\n"; } </massage> __infile__ blah blah blah #BG blah blah blah blah blah blah #EN blah blah blah blah #BG blah #EN blah blah blah blah __infile.out__ #BG blah blah blah blah blah blah #EN #BG blah #EN

      --

      flounder