Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing user from text file

Status
Not open for further replies.

FurryGorilla

Technical User
Apr 11, 2001
76
GB
Hi I'm having a slight problem with something that seems to be quite common but I still can't find a solution :-(

Basically I have a form which passes a user name to a script which in turn compares these against a text file:

[tt]user1:pass1:
user2:pass2:
user3:pass3:[/tt]

OK so I can read in the data from the form but how can I compare this against a line in the text file and then remove this line if the user name matches against one in the text file?

The code I have is as follows (goBoating thank you ;-)):

[tt]#!/usr/bin/perl

print "Content-type: text/html\n\n";

use CGI;
$cgi = new CGI;

$pwd_file = "pwd.txt";
$new_pwd_file = "temp.txt";

$uname = $cgi->param('username');
$pword = $cgi->param('password');

open (USERS, $pwd_file);
while (<USERS>) {
$buffer .= $_;
}
close USERS;

print &quot;$buffer.\n&quot;;

@lines = split(/\n/,$buffer);

$line = &quot;$uname:$pword:&quot;;

print $line;

push @lines, $line;

print @lines;

open (PWD, &quot;>>$new_pwd_file&quot;);

foreach $line (@lines) {
print PWD &quot;$line\n&quot;;
}
close PWD;[/tt]

As you can see I'm adding the user name and password onto the line and then writing to a different file. I'd prefer to overwrite the original file or possibly delete and rename. I'm just learning so I'm not sure about the splice command which seems to be popping up :)

Thanks in advance
Chris
 
Chris, you can skip the old line when reading the file in using:
Code:
while(<USERS>) {
    next if /^$uname:/;
    $buffer .= $_;
}
Or alternatively, when you are writing the file back out:
Code:
$line = &quot;$uname:$pword:&quot;;
foreach(@lines) {
    next if /^$uname:/;
    print PWD &quot;$_\n&quot;;
}
print PWD &quot;$line\n&quot;;
close PWD;

Hope this helps :) Cheers, NEIL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top