Deleting - Delete a line of text for a .txt file

They have: 62 posts

Joined: May 1999

Hi-
I'm using PERL, and I need to delete a line of text from a database (.txt file) that is specified. How do I search for this line, and then delete it?

----------
Casey Hansen
www.caseynet.com

They have: 5,633 posts

Joined: Jan 1970

Try this code:

open(FILE, '$sites') die $!;
@lines = <FILE>;
close(FILE);
$search_string = $sitename;
open(FILE, '>$sites') die $!;
for (@lines) {
print FILE $line,"\n" unless $_ =~ /$search_string/;
}
close FILE;

Cheers

----------
wdresources
http://www.wdresources.com

They have: 5,633 posts

Joined: Jan 1970

Oopss ... BT doesn't like pipes ...
add "or" (without commas) before "die $!"; (both)

Cheers

----------
wdresources
http://www.wdresources.com

They have: 4 posts

Joined: May 1999

open(FILE, 'file.txt')die("error");
@lines = <FILE>;
close(FILE);
$search_string = "Searching for";

open(FILE, '>file.txt')die("error");
foreach $line (@lines){
if (!$line ne $search_string){
print FILE "$line\n";}
}
close(FILE);
print "done";

*Excuse the smilies*

That's about it... (correct me if I'm wrong)

They have: 2,390 posts

Joined: Nov 1998

Just in case you are not familiar with this problem:
- each smiley replaces the ':' and the ')' characters.
JP

----------
The Webmaster Promotion and Resource Center.
http://www.what-next.com

They have: 62 posts

Joined: May 1999

Hi-
Thank you for the help! I'll give it a go......

----------
Casey Hansen
www.caseynet.com

They have: 62 posts

Joined: May 1999

Hi-
I tried out the code, but it did not delete the line of text that was searched for. Here's what I used:

open(FILE, '$sites') or
die("Couldn't open database, sorry");
@lines = <FILE>;
close(FILE);
$search_string = "$sitename";
open(FILE, '>$sites') or
die("Sorry, couldn't open");
foreach $line (@lines) {
if (!$line ne $search_string){
print FILE "$line\n";}
}
close(FILE);

Any ideas would be great! Thanks! Smiling

----------
Casey Hansen
www.caseynet.com

They have: 5,633 posts

Joined: Jan 1970

of course it doesn't work, smileys are not parsed by perl! Smiling
serios now, it is probably a line-feed problem, try this:

$search_string = "$sitename\n";

cu
patrick

----------
http://www.allcgi.de
not quite up yet, but see if you can find something for you!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.