in reply to Text file (Passwd file) record editing
#!/usr/bin perl -w use strict; my $passfile = shift || 'passfile'; my $wantedname = shift || 'John Doe'; my $newpass = shift || '1234'; # example for changing a value my $tempfile = $passfile.'temp'; # file where we copy all info for rem +oving a line my $searchstr = shift || 'abcd'; my $line; # line of the passfile #first open the file open(PFILE,"<$passfile") or die "unable to open $passfile, because $!\ +n"; #second open our tempfile open(TEMP,">$tempfile") or die "unable to open $tempfile, because $!\n +"; while ($line = <PFILE>){ if ($line =~m/$wantedname/){ print "Changing $line to "; #assume we now that we want to change the second element if we don +'t a comparison will be easily implemented if($line =~s/$searchstr/$newpass/){ #put data back together print $line."\n"; # display line on screen } } print TEMP $line; # write line to temp file } #close original file close PFILE or die "unable to close $passfile,because $!\n"; die "unable to delete $passfile, because $!\n" if (!unlink($passfile)) +; # rename temp file to original file rename $tempfile, $passfile or die "unable to rename file, because $!\ +n"; #done print "\ndone.\n";
This does not work if the name is not unique or the value that you want to chang isn't.But anyway it is something :-)
Hope this helps,
C-Keen
|
|---|