Removing range of lines from a file

Does anyone know of a way to remove a range of lines from a file within a perl script. The 'to' and 'from' line numbers are held in 2 variables.
Any help on this would be much appreciated
hally44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rugdogCommented:
$from=5;
$to=20;
open(F,"file") or die "failed to open file: $!\n";
while(<F>){
    print if($. < $from || $. > $to);
}
close(F);
Dave CrossModule AuthorCommented:
The flip-flop operator is useful for things like this.

This example is used as a filter (i.e. it reads from STDIN and writes to STDOUT).

my ($from, $to) = (5, 30);

while (<>) {
  print unless $. == $from .. $. == to;
}

If you can hard-code the range then it gets even easier

while (<>) {
  print unless 5 .. 20;
}


Dave...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
inq123Commented:
open(IN, "$file");
open(OUT, ">$file-new");
@lines = <IN>;
print OUT @lines[0..$from-2] if($from > 1);
print OUT @lines[$to,@lines-1];
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Dave CrossModule AuthorCommented:
In general, reading the whole file into an array if you can process it a line at a line.
inq123Commented:
What did you want to say davorg?  Were you trying to comment on efficiency?

BTW, change last line of my code above to

print OUT @lines[$to..@lines-1];
Dave CrossModule AuthorCommented:
Oops, not sure what happened there.

I was trying to say that it's a bad idea to read a whole file into memory if you can process it a line at a time. If the file is large you can be wasting a lot of memory.
rugdogCommented:
hally44,  if you're in unix you could also try sed:

sed '2,10d' < file.txt

to delete lines 2-10 (inclusive) from file.txt
jmcgOwnerCommented:
Nobody has mentioned the Tie::File way to do this.

    use Tie::File;

    tie @array, 'Tie::File', filename or die ...;

    splice @array, $from, $to-$from;

(Do I have an off-by-one error?)



jmcgOwnerCommented:
Correcting the off-by-one error now that I have a chance to think about it:


use Tie::File;

tie @arry, 'Tie::File', $filename or die "Open failed on $filename: $!";

splice @array, $from-1, $to-$from;

untie @array;

This will modify the file in-place. It does not load the whole file into memory, so it will still work for very large files, but it will necessarily read in the entire file and rewrite the contents from the position of the deleted lines.
hally44Author Commented:
Thanks to everyone for their advice. The file I am editing is very large so therefore took the non array solution but have tried to split the points between all contributors. Many thanks again for your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.