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""
{next;}
my @data = split(/\:/, $_);
if $data[0] = $user {
$checkuser = "1"
print LIMIT "
}
}
# ----------
# if checkuser flag not set to 1 then add user to file
# ----------
if ($checkuser = "0"
{print LIMIT "$user\:$access\n";}
close(LIMIT);
#--------------- End of script --------------------
How do I achieve this?
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"
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""
my @data = split(/\:/, $_);
if $data[0] = $user {
$checkuser = "1"
print LIMIT "
}
}
# ----------
# if checkuser flag not set to 1 then add user to file
# ----------
if ($checkuser = "0"
close(LIMIT);
#--------------- End of script --------------------
How do I achieve this?