Gossamer Forum
Home : General : Perl Programming :

delete a line from a flat db file

Quote Reply
delete a line from a flat db file
Hi
How can I delete the first line from a flat db.file?
Also I have to find a line (as $line) in the file and delete it... removing spaces/blank lines.
Can you help me understand this function?
Thanks.
Quote Reply
Re: delete a line from a flat db file In reply to
I think you are asking how to delete the first line and any blank lines correct?

Here goes:
Code:
open(FILE,"/path/to/file") | | die "can't open file";
@contents = (<FILE> );
close(FILE);

$count_lines=0;
foreach $line (@contents) {
$count_lines++;
chomp $line;
if ($count_lines == 1) {
$line = '';
}
if ($line ne '') {
$line = "$line\n";
push(@new_contents, $line);
}
}

open(FILE,">/path/to/file") | | die "can't open file";
print FILE @new_contents;
close(FILE);
What this is doing:
Opening the file for reading, putting the contents of the file into an array(memory) called @contents.
We create a variable to keep count and we set it's value to 0. Then, start a loop that pulls out each line for analyzing. We assign the variable $line to each line. We need to remove the newline characters (\n) from each line, so we use the chomp command. Then we itenerate the count using ++.

Now we find out what line we are on by checking where our our count is. Then we simply leave out of our new array waht we don't want and print it back to a file.

Chris Ellenburg,
webmaster@wfmy.com



------------------
WFMY-TV Webmaster

[This message has been edited by Chris071371 (edited November 20, 1999).]
Quote Reply
Re: delete a line from a flat db file In reply to
Nice code Chris, Robert, don't forget to delete the space between the two | | in Chris' code, this bbs software does that.
Quote Reply
Re: delete a line from a flat db file In reply to
Great it works!!! but what a shall do is this Frown
I shall get a line through a query_string and remove the value from the file.

for example I must call the

scritp.cgi?line=value

...find and remove value. Any suggestions?

Thanks!
Quote Reply
Re: delete a line from a flat db file In reply to
Opps ..sorry I got how to do it.
Again, thanks very much
Quote Reply
Re: delete a line from a flat db file In reply to
I have a problem. The script don't find these passwords if a cut and paste from another file into my database file. I guess it is due to something in the original file. Maybe invisible signs generated???


FH42n48Le7gRSk105JBjn3Bm1731jOU1
XuLTplCwgfo7qq3nGR100ge5qr7pCI73
a6cx54r3oj733OprAOLVEY151sQcNTk2
1luCOl2xw5AAA4WWuM7PJrcm67nQyEc1
47T166W464oe16hpp28oYchEwoBdL9an
NPLiQsmy9R78VpOU31C3cUGDkoyQw3sl
p4cM67042VA4nN89D04Chn2Dxp81h8Kj
7GurQs6z5zgD24c1X9SV42f146Sr720i
naZl41939LHaTM5RI13EQ4X63soUMF2R
Quote Reply
Re: delete a line from a flat db file In reply to
How exactly are you looking for them? Please paste your code here or email it to me at chris@wfmy.com.

------------------
WFMY-TV Webmaster