in reply to More efficient way to exclude footers
EDIT:
Well I feel silly. As AnomalousMonk below mentioned, I had (unintentionally) provided the same solution as Athanasius did earlier. I could have sworn Athanasius' solution involved reading in the file all into an array before processing. That is what I get for not reading all the replies carefully I suppose.
Original reply spoilr'd to avoid polluting the thread with redundant bits (Unless there is a "Delete Post" gubbin I somehow missed?)
Just to build off what ikegami mentioned earlier.
Here is a way to use a sliding window/rolling buffer to process the file and skip the required lines if the file is too large to be read into memory all at once:
#!/usr/bin/perl use 5.018; use strict; use warnings; my $header_lines = $ARGV[0] // 0; my $footer_lines = $ARGV[1] // 0; # skip header <DATA> for 1 .. $header_lines; # print everything but the footer lines my @buffer; while ( my $line = <DATA> ) { chomp $line; push @buffer, $line; say shift @buffer if scalar @buffer > $footer_lines; } __DATA__ header 01 header 02 line 01 line 02 line 03 line 04 line 05 footer 01 footer 02 footer 03
Output:
blingy > buffer.pl 2 3 line 01 line 02 line 03 line 04 line 05 blingy > buffer.pl 1 1 header 02 line 01 line 02 line 03 line 04 line 05 footer 01 footer 02 blingy > buffer.pl 2 line 01 line 02 line 03 line 04 line 05 footer 01 footer 02 footer 03 blingy > buffer.pl 0 3 header 01 header 02 line 01 line 02 line 03 line 04 line 05 blingy > buffer.pl header 01 header 02 line 01 line 02 line 03 line 04 line 05 footer 01 footer 02 footer 03
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: More efficient way to exclude footers
by AnomalousMonk (Archbishop) on Aug 19, 2015 at 21:48 UTC | |
by ateague (Monk) on Aug 19, 2015 at 22:09 UTC |