You could be scanning through the file line for line and look for the name you want, (use the matching operator), split the line with 'split' modify your information delete the old line and add the new one. This might look something like this:

#!/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


In reply to Re: Passwd file by C-Keen
in thread Text file (Passwd file) record editing by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.