
unknownSym
stranger
Feb 15, 2001, 12:24 PM
Post #2 of 5
(488 views)
|
|
Re: Removing a single line from a file
[In reply to]
|
Can't Post
|
|
If you're not worried about concurrent access to the file then you can do this. my $delete_id = "0002"; # open your database file. open(FILE, "<filename.ext") || die("Could not open database file."); # open your output file, because you don't want to write read and write from/to a # file with variable length records in it. open(OUTPUT, "<output.ext") || die("Could not open output file."); my $line = ""; while($line = <FILE>) { my @data = split(/\|\|/, $line); # split your data up my $id = $data[0]; # just for readability, store your 'id' in $id. # compare to the record to delete, if a match then don't write it to the output file. next if($id eq $delete_id); print OUTPUT join("||", @data); # join the data of lines that pass and then print them. } # end while loop. close FILE; close OUTPUT; rename(output.ext, filename.ext); # overwrite your database file with the output file.
|