Gossamer Forum
Home : General : Perl Programming :

Text File Editing

Quote Reply
Text File Editing
Hello ,

I am looking for the best / most secure way to delete from a TEXT file.

My .htpassword has over 4000 members it in and I am looking to edit though them in a small administration.

Each line in the file is like so...

user@name.ca:6b5DLG7wA/HFM
geek@nerds.com:dfsCA/5K7O3Og



Is there a better way to delete from a TEXT file instead of just running though the @ARRAY and re-printing it into the file .htpassword ? with 4000 members + i dont want to damage the files contents in any way.



Any suggestions...
Quote Reply
Re: [ZimZangZoom] Text File Editing In reply to
TextPad...http://www.textpad.com
EditPlus...http://www.editplus.com
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [ZimZangZoom] Text File Editing In reply to
How about vi, emac, sed, ed and perl?

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [ZimZangZoom] Text File Editing In reply to
Ar you intending to do this manually, or do you specifically need a script?

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: [A.J.] Text File Editing In reply to
AJ:

Wouldn't your script called "Ace Password" do what he needs to do?

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Text File Editing In reply to
Yup, sure would. Its here: http://www.ace-installer.com/perlacepassword.php. Should do what he is asking, may wanna add in something to create a backup .htpasswd file though, as that is a lot of password to be crawling through Tongue

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: [A.J.] Text File Editing In reply to
Anyone got a good example of how to delete one member (in Perl) out of a text file ?

Something that I can trust will do the job.

Currently I just open .htpassword and reprint the WHOLE array into the file after i edit the array. Is that the only and best way ?
Quote Reply
Re: [ZimZangZoom] Text File Editing In reply to
Its no where near the most effecient code, but it works Tongue, try this;
Code:
# the sub that will delete usernames from the file!
sub delete_username {

open(DEL, ".htpasswd") || die &error("Unable to open .htpasswd file. Reason : $!");
@check = <DEL>;
close(DEL) || die &error("Unable to close .htpasswd. Reason: $!");

foreach $line (@check)
{
my ($username, $password) = split(/:/, $line);
if ($username eq $delusername) { $status = "ok"; last; } else { $status = "not found"; }
} # end foreach loop

if ($status eq "not found") { &error("Username not found! You can only edit an existing username!"); }

# If we get to here then the username exits, so we need to do the foreach loop to add all usernames and possword EXCEPT this one!

open(DEL, ">.htpasswd") || die &error("Unable to open .htpassd. Reason: $!");

foreach $line (@check) {

my ($username, $password) = split(/:/, $line);

if ($username eq $delusername) { } else { print DEL "$username:$password"; }

} # end foreach loop

close(DEL) || &error("Unable to close .htpasswd after username deletion. Reason:L $!");


print "Username $delusername was deleted successfully!";

} # end the delete sub

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: [A.J.] Text File Editing In reply to
ACK! You can shorten that code quite a bit with a little effort.
Code:
sub delete_user {
my ($user, $found) = (shift, 0);
open (HTPW, ".htpasswd") or die $!;
open (TMP, ">pass.tmp") or die $!;
flock (TMP, 2) or die $!;
(/^\Q$user:/ ? $found++ : print TMP) while (<HTPW>);
close TMP;
close HTPW;
rename("pass.tmp", ".htpasswd") or die $!;
print $found ? "user deleted successfully" : "user not found";
}

--Philip
Links 2.0 moderator
Quote Reply
Re: [A.J.] Text File Editing In reply to
Quote:
if ($username eq $delusername) { } else { print DEL "$username:$password"; }

Ewww I hate stuff like that. Why do you do it? If anything, why not just use:

Code:
unless ($username eq $delusername) {
print DEL "$username:$password";
}

Here is a quick and dirty way.

Code:
open F, ".htpasswd" or die $!;
my $string = do { local $/; <F> };
close F;

$string =~ s/\Q$some_user\E:.*?\n//;

open F, ">.htpasswd" or die $!;
flock(F, 2) unless $^O eq 'MSWin32';
print F $string;
close F;

Last edited by:

Paul: Mar 26, 2002, 4:12 AM
Quote Reply
Re: [Paul] Text File Editing In reply to
Man....didn't I just say it was crap code! I don't really need critasism to tell me that! I'm not even learning Perl any more...only just writing with what I knew....and that code was written a good few months ago Wink

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: [A.J.] Text File Editing In reply to
Hmm if in your own words it is "crap code" why provide it to someone to use?

>>
I don't really need critasism to tell me that!
<<

I don't believe I criticized it.

I said I hate stuff like meaning that in my personal opinion using { } is unnecessary. I didn't comment on the quality of the code.

Last edited by:

Paul: Mar 26, 2002, 4:56 AM
Quote Reply
Re: [Paul] Text File Editing In reply to
Its crap code compared to what i would write myself NOW! As I said, it was written a while ago, so my skills have improved somewhat. Think what you want. but I can't be arsed to argue with you today (its been another one of those days so far) Frown

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: [A.J.] Text File Editing In reply to
You are barking up the wrong tree Unsure. Im not arguing or criticizing your code. It was just a suggestion that you could use unless instead of empty { }'s ....its a subjective opinion (although it is better coding practice too).

I just wouldn't offer code to someone and then label it as crap, it kinda defeats the purpose. Smile

Last edited by:

Paul: Mar 26, 2002, 5:03 AM
Quote Reply
Re: [Paul] Text File Editing In reply to
Yes, I know I could make it better. WHEN I find free time to do it, I will [;)} I could have used your example, or;

if (!$username eq $delusername) {
print DEL "$username:$password"; }

OR

if ($username ne $delusername) {
print DEL "$username:$password"; }

Or any other methods, but at the time of writing it I didn't know those methods. Anyway, the only reason I was showing it was, cos its easy to see what is happenening in the code, and he asked for an example. He didn't say it had to be a good one Wink

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: [A.J.] Text File Editing In reply to
>>
if (!$username eq $delusername) {
print DEL "$username:$password";
}
<<

Hmm not sure that would work.