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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update data in a file

Status
Not open for further replies.

visualJay

Programmer
Jun 14, 2001
57
US
I am trying to open a file and change the data in the middle of a file.

Example File

johndoe:web:2:email:4:ftp:4
janedoe:web:5:email:6:ftp:1
mikedoe:web:3:email:1:ftp:6

I want to beable to read the file see if the user is there
like janedoe if it is then change the 5 to an 8 and so on
if user not there then I want to append the data at the end.

# -------- start of script --------------
my $user = $ARGV[0]
my $access = $ARGV[1]
my $checkuser = "0"


open (LIMIT, ">>$flimit") or die "Couldn't open $flimit for writing: $!\n";
while(<LIMIT>){
chomp;

#------------------------------
# This is were I am checking the user and if exists edit the line
# although I am not sure on how to edit just a single line or if
# this is correct way about doing it or if this will cause problems
# I need to make data[1] = $access then write data[1] back or even write all of data back
#------------------------------
if($_ =~/^\#/ || $_eq&quot;&quot;){next;}
my @data = split(/\:/, $_);
if $data[0] = $user {
$checkuser = &quot;1&quot;

print LIMIT &quot;
}
}
# ----------
# if checkuser flag not set to 1 then add user to file
# ----------

if ($checkuser = &quot;0&quot;){print LIMIT &quot;$user\:$access\n&quot;;}
close(LIMIT);

#--------------- End of script --------------------

How do I achieve this?
 
simple, rewrite the file instead of appending. first open/append the file, take the information out. open the file for writing, and repost the information in.

open (LIMIT, &quot;>$flimit&quot;) or die &quot;Couldn't open $flimit for writing: $!\n&quot;;

#checks to see is $user is null
if ($user eq &quot;&quot;) { the user exits, so change the variable } else { the user does not exist, do not change variable }

than you can rewrite the data into the file, now that the variable has changed.
 
#checks to see is $user is null
if ($user eq &quot;&quot;) { the user exits, so change the variable } else { the user does not exist, do not change variable }

I understand the concept just not sure how to write(change the date in the variable. I will read it all in to the variable $flimit how do i find the user that will be say 7 lines down in the file and change the variable 2 or 4 over.

 
Code:
my ( @data, @info, $newvalue, $checkuser );

open FILE, &quot;< $flimit&quot; or die &quot;Could not open $flimit: $!&quot;;
chomp ( @info = <FILE> );
close FILE;

foreach ( @info ) {
    @data = split /\:/;
    if ( $data[0] eq $ARGV[0] ) {
        $data[2] = $ARGV[1];
        $checkuser++;
    }
}

#if user was not seen, add this new user...
$newvalue = &quot;$ARGV[0]\:$ARGV[1]&quot;;
if ( !$checkuser ) { push @info, $newvalue; }

open NEWFILE, &quot;> $flimit&quot; or die &quot;Could not open $flimit: $!&quot;;
print NEWFILE &quot;$_\n&quot; foreach @info;
close NEWFILE;[code]

Not tested.  But I think this will work. *crosses fingers* [wink]

HTH. Notorious P.I.G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top