Gossamer Forum
Home : General : Perl Programming :

Delete A Certain Line From File

Quote Reply
Delete A Certain Line From File
I am having trouble on deleting a certain line from a text file. I want to be able to find a certain word on that line in the text file and be able to delete that whole line. Its for a password protecting script. A line of info from the txt file is

username|password|email|otherinfo

So what i am trying to do is be able to select a user name from a pull down menu(got this far) then be able to delete that whole line that there username lies on.

Kurt
Quote Reply
Re: [kurtis_m] Delete A Certain Line From File In reply to
One way to do this is to first open the database for reading. Then open a temporary file for writing. While you loop over the database, do a regex (or substring or whatever floats your boat) on the raw line for the username and write to the temporary file for any line that doesn't match. You might want to add a flag so that you don't waste time on trying to match the line on the remainder of the file once you've already found it. Once done, backup your original file, then rename the temporary file to your permanent filename.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [kurtis_m] Delete A Certain Line From File In reply to
Something like;

Code:
open(IN, "/path/to/file.txt") || die "Can't open, reason: $!";

while (<IN>) {

if ($_ =~ /^$user/i) {
# skip it, cos we dont want ot add this line back ;)
} else {
# add it back to the main var, ready to be put in the file again...
$back_in = $_;
}

}

close(IN);

open(OUT, ">>/path/to/new.txt") || die "Can't read. Reason: $!";
print OUT $back_in;
close(OUT);

`rm /path/to/file.txt`; # remove the old one first...
`mv /path/to/new.txt /path/to/file.txt`; # rename the new one, to the old ones name...so we get the updated version ok :)

Hope that helps Smile (untested, just wrote it quickly to show you the idea)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Delete A Certain Line From File In reply to
yeah, except you should just print to the new file immediately instead of wasteing memory....

Code:
open (OLD, "/path/to/old.txt") or die $!;
open (NEW, ">/path/to/new.txt") or die $!;
while (<IN>) {
if (! $found) {
$found++ if /^\Q$user|/;
}
else {
print NEW;
}
}
close NEW;
close OLD;
rename ("/path/to/db.txt", "/path/to/db.txt.bak");
rename ("/path/to/new.txt", "/path/to/db.txt");

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [kurtis_m] Delete A Certain Line From File In reply to
You should read the whole of perlfaq5 at perldoc.com but specifically.....

http://www.perldoc.com/...beginning-of-a-file-
Quote Reply
Re: [Andy] Delete A Certain Line From File In reply to
Andy your code would delete everything except one line as you are just overwriting $back_in on every iteration. You should also not use system commands when you can simply use rename() and unlink()

Empty blocks are also ugly and you should avoid them, if you need to skip lines use a next;

Last edited by:

Paul: Nov 23, 2002, 2:30 AM
Quote Reply
Re: [fuzzy thoughts] Delete A Certain Line From File In reply to
Andrew,

What does $found and $user refer too? In the code:
Code:
open (OLD, "/path/to/old.txt") or die $!;
open (NEW, ">/path/to/new.txt") or die $!;
while (<IN>) {
if (! $found) {
$found++ if /^\Q$user|/;
}
else {
print NEW;
}
}
close NEW;
close OLD;
rename ("/path/to/db.txt", "/path/to/db.txt.bak");
rename ("/path/to/new.txt", "/path/to/db.txt");
Thanks
Kurt
Quote Reply
Re: [kurtis_m] Delete A Certain Line From File In reply to
$found is a 0/1 flag to signify whether the match has already occured. You'll notice that once $found is incremented, all lines are simply written to the new file without bothering to match on the line. $user refers to the username of the record you want to remove.. Note that you also want to use flock().

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] Delete A Certain Line From File In reply to
you can do it without tmp file.

open(F, "+< $ufile") or &write_some_log_sub;
my $out = '';
while (<F>) {
/^$username/ && next;
$out .= $_;
}
seek(F, 0, 0) or &write_some_log_sub;
print F $out or &write_some_log_sub;
truncate(F, tell(F)) or &write_some_log_sub;
close(F) or &write_some_log_sub;

if it's production, obviously there are things like logging every change to the file for undo purposes and keeping backups for [n] days....
Quote Reply
Re: [adrockjames] Delete A Certain Line From File In reply to
I have modified your program and tried to delete records, but without luck (nothing happens)!? I want to delete the specific lines in “lightbox_images.txt” containing “$image”. Path and CHMOD is correct.

Here is the modified code:

$ufile = "lightbox_images.txt";

open(F, "+< $ufile");

my $out = '';

while (<F>) {

/^$image/;

$out .= $_;

}

seek(F, 0, 0);

print F $out;

truncate(F, tell(F));

close(F);
Quote Reply
Re: [rompole] Delete A Certain Line From File In reply to
You could do as I suggested and use the tried and tested code provided by the perl gurus at perldoc.com Tongue