in reply to Re: Extracting a block of text between start and end point
in thread Extracting a block of text between start and end point

Thanks a lot sir, but then in this case, how do I generalise this for all my files. What i understand is I need to copy paste all files below _data_ wont that be too much of manual work ? sorry for being too naive i really am a beginner here
  • Comment on Re^2: Extracting a block of text between start and end point

Replies are listed 'Best First'.
Re^3: Extracting a block of text between start and end point
by Athanasius (Archbishop) on Jun 17, 2015 at 06:50 UTC

    My code was just a proof-of-concept to show how to use the range operator for this task. For a working script operating on files “log1.log”, “logX.log”, and “logZ.log”, say, — and assuming you want all the output to go to a single file “output.txt” (do you?) — you would do (untested):

    my @filenames = qw(log1.log logX.log logZ.log); open(my $out, '>', 'output.txt') or die "Cannot open file 'output.txt' + for writing: $!"; for (my $filename (@filenames) { open(my $in, '<', $filename) or die "Cannot open file '$filename' +for reading: $!"; while (<$in>) { ... if (/AS \s+ $keyword/ix) { print $out join('', @block); last; } ... } close $in or die "Cannot close file '$filename': $!"; } close $out or die "Cannot close file 'output.txt: $!";

    See perlintro and perlopentut.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^3: Extracting a block of text between start and end point
by QM (Parson) on Jun 17, 2015 at 07:51 UTC
    I normally write my IO scripts to take files on the command line, and output to STDOUT. (That makes the script a filter, and all kinds of wonderful proceed from there.)

    So instead of opening an output file, and instead of looping over a list of files, do this:

    while (<>) { do_something_here(); print $blah if $accepted; }

    Then you run it like so:

    my_script blah*.log > my_output_file

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re^3: Extracting a block of text between start and end point
by robby_dobby (Hermit) on Jun 17, 2015 at 06:52 UTC

    One of the things perl does really well is I/O, that is - it's really good at reading files and extracting data from them. It does it so well that speed is not even a consideration, in most cases. :-)

    Like Athanasius said, you can keep of all that log content in their own files and read them from perl code. Then, you can use the Flip-flop operator as shown by Athanasius, reworking them to your needs. For starters, look at open and work from there. :-)