Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting a line from a file

0 views
Skip to first unread message

Sebastian Young

unread,
Aug 11, 2001, 6:27:29 AM8/11/01
to
I'm still having real trouble deleting a line from a file. I've read the
article on Perldoc, but it doesn't help me. Does anyone know how to do it?


Sebastian Young

unread,
Aug 11, 2001, 9:53:24 AM8/11/01
to

brian d foy

unread,
Aug 12, 2001, 10:54:01 AM8/12/01
to
In article <9l31ek$kbd$1...@neptunium.btinternet.com>, "Sebastian Young"
<sebasti...@talk21.com> wrote:

> I'm still having real trouble deleting a line from a file. I've read the
> article on Perldoc, but it doesn't help me. Does anyone know how to do it?

what part of the answer is giving you trouble?

--
brian d foy <com...@panix.com>
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html

Matthew Frick

unread,
Aug 20, 2001, 11:40:27 PM8/20/01
to

"Sebastian Young" <sebasti...@talk21.com> wrote in message
news:9l3djf$58g$1...@neptunium.btinternet.com...

> I'm still having real trouble deleting a line from a file. I've read the
> article on Perldoc, but it doesn't help me. Does anyone know how to do it?
>

a simple way...


open file

read the file into an array.

delete the line you want removed

shuffle rest of file up to fill empty array element

write array to file (not append but overwrite)

close file

done.


andreas

unread,
Aug 24, 2001, 11:19:44 PM8/24/01
to
In article <3b81d771$1...@news.chariot.net.au>,

Matthew Frick <mfr...@chariot.net.au> wrote:
>
>> I'm still having real trouble deleting a line from a file. I've read the
>> article on Perldoc, but it doesn't help me. Does anyone know how to do it?
>
>a simple way...
>open file
>read the file into an array.
>delete the line you want removed
>shuffle rest of file up to fill empty array element
>write array to file (not append but overwrite)
>close file
>done.

how about something like:

system("cp $file $file.old");
open(OLD, "$file.old");
open(NEW, ">$file");
while (<OLD>) {
print NEW unless (/$pattern/ || $. == 0); # delete matching line, if
# any, and first line
};
close(OLD);
close(NEW);

you may wish to edit the file in place. see the perl cookbook
for an answer.

George Bouris

unread,
Aug 26, 2001, 4:57:11 PM8/26/01
to
Just use this sub
Do not use the same source file as destination!
#####################
#&delete_line(line to delete , source, dest
&delete_line(3,"/test.txt","/test.txt");

sub delete_line{
($line,$source,$dest)=@_;

open (FILE,"<$source") || die $!;
open (FILE2,">$dest") || die $!;
while (<FILE>){
$i++;
(print FILE2 $_) unless ($i==$line);
}

close (FILE);
close (FILE2);
}

andreas

unread,
Aug 26, 2001, 8:34:50 PM8/26/01
to
In article <9mbnq9$9sd$1...@usenet.otenet.gr>,
George Bouris <gbo...@toxo.com> wrote:
>
>Just use this sub

hm, why is this an improvement over what i posted?

0 new messages