Reading a line and then deleting the line

How do i READ A LINE from a file and then after reading it, then delete that line....  assuming I have a bunch of lines...

thanks
CODERAsked:
Who is Participating?
 
maneshrConnect With a Mentor Commented:
here is a script that will do the above for you.

basically this script reads a file and picks all the lines that start with a # (ignoring/deleting the other lines) and write those lines back to the same file.

That in effect simulates a delete of specific lines.

you can replace the if with any condition that you are looking for.

======================coder.pl
#!/usr/local/bin/perl

$file="/tmp/file.txt";
open(FILE,"$file") || die $!;

while(<FILE>){
  if (/^#/){ ## The line starts with a #
    push(@arr,$_); ## Store that line in an array
  }
}
close(FILE);

open(FILE,">$file") || die $!;
foreach(@arr){
  print FILE $_;
}
close(FILE);
0
 
lambdaCommented:
if (/^#/){

maneshr: I think this line has to be modified. CODER wants some particular lines to be deleted, so it shld be quite the opposite check. Something like, lines starting with '#' are to be deleted.
 
0
 
maneshrCommented:
lambda,

like i said in my comment ...

"you can replace the if with any condition that you are looking for. "

the script is just a sample that shows one how to do the "delete".
i have left the exact condition for the delete to the user.

i would suggest that we leave the interpretation of the delete to CODER.

if CODER can provide the exact condition, only then can i modify the if statement.

Rgds
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
maneshrCommented:
CODER,

was the solution that i gave you not up to your requirements??

what additional items were you looking for in your script??

do let me know.
0
 
CODERAuthor Commented:
it was cool naneshr... thanks.,.
0
 
maneshrCommented:
glad to know that.

the reason i asked the same was because you had graded the answer B.
therfore i was wondering if i had missed out on some point.

Rgds
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.