Francesco Guglielmo wrote:
>
> I have many files with username and passwd.The old files are in format:
>
> qw001234 asdfgh # all usernames are composed by qw/po/zx + 00 + 4
> number
>
> The new files have three usernames and passwd for each number on each
> line:
>
> qw009876 qwerty po009876 poiuyt zx009876 mnbvcx
>
> To patch the old files someone before me made a new file with the other
> "po" and "zx" but with a big error.The format is
>
> qw001234 po001234 ghjkld zx001234 tgbnhy
>
> There's not the passwd for the user in the old file(ever qw).Now I need
> just one file with in the format
>
> qw001234 qwerty
> qw009876 qwerty
> po001234 ghjkld
> po009876 poiuyt
> zx009876 mnbvcx
> zx001234 tgbnhy
This will work with the supplied data:
#!/usr/bin/perl -w
use strict;
my %users;
while ( <DATA> ) {
chomp;
my @data = split /\s*((?:qw|po|zx)00\d{4})\s*/;
# remove the first field
shift @data;
while ( @data ) {
my ( $user, $pw ) = splice @data, 0, 2;
# don't assign a password if one already defined
$users{ $user } = $pw unless defined $users{ $user };
}
}
print "$_ $users{$_}\n" for sort keys %users;
__DATA__
qw009876 qwerty po009876 poiuyt zx009876 mnbvcx
qw001234 po001234 ghjkld zx001234 tgbnhy
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]