On 6/16/10 Wed  Jun 16, 2010  1:29 PM, "little_help"
<littlehelph...@gmail.com> scribbled:

> Is there a way to change all entries for a current username with a new
> username?  I have two files (A and B for the sake of argument).  File
> A has all the current names in the passwd map and file B has the new
> names.  For example
> File A    File B
> Foo        Foo123
> Bar         Bar1234
> 
> I need to change all entries in File A with the entries in File B.
> 
> ORIGINAL FILE
> foo:xxxx:111:222:Foo User:/home/foo:/bin/ksh
> 
> NEW FILE
> foo123:xxxx:111:222:Foo User:/home/foo123:/bin/ksh
> 
> I need some type of loop that will perform an exact match and run
> through both files?  Any help would be appreciated.
> 

How are the entries in file B matched up with the corresponding entry in
file A? Is it by the position in the file or by the similarity in the names?
Either method can have problems.

I would recommend putting the old and new names in one file, one pair per
line, separated by whitespace:

Foo Foo123
Bar Bar1234

You can then read in the file and create a hash giving the conversion from
old name to new (untested):

my %names;
open( my $in, '<', $filename ) or die("Can't read $filename: $!");
while(<$in>) {
  chomp;
  my($old,$new) = split;
  $names{$old} = $new;
}
close($in);

Then you can read the passwd file and substitute the new names for the old
names by using the %names hash.

If putting the names in one file is not possible, you can open the two
files, read one line from each file, chomp the inputs to remove the line
endings, and put the names in the hash as above.




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to