in reply to Deleting first and last TWO(2) lines of a text file
Here is a relatively simple generic solution to be tailored to your specific requirements. I've named it headtail.pl but you can call it whatever you want.
#!/usr/bin/perl -Tw use strict; use warnings; my $skiphead = 1; my $skipfoot = 2; my $infile = 'test.txt'; my $outfile = '/tmp/out.txt'; my @buffer = (); open IN, '<', $infile or die "Cannot open $infile for reading: $!"; open OUT, '>', $outfile or die "Cannot open $outfile for writing: $!"; <IN> for (1..$skiphead); push @buffer, scalar <IN> for (1..$skipfoot); while (<IN>) { print OUT shift @buffer; push @buffer, $_; } close OUT; close IN;
$ cat test.txt 1 hello 2 there 3 this 4 is 5 a 6 good 7 test $ ./headtail.pl $ cat out.txt 2 there 3 this 4 is 5 a
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 14:02 UTC | |
|
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 16:08 UTC | |
by hippo (Archbishop) on May 16, 2014 at 16:17 UTC | |
by vsmeruga (Acolyte) on May 16, 2014 at 16:49 UTC | |
by vsmeruga (Acolyte) on May 16, 2014 at 16:42 UTC | |
|
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 16:14 UTC |