Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get string in a file and find string appropriate in another file

Posted on 2011-02-21
4
Medium Priority
?
272 Views
Last Modified: 2012-05-11
I has two file: user.txt and userpass.txt
content of user.txt:
##########
perter
john
marry
chichi
marche
tuto
ruco
###########
And content of userpass.txt:
###############
tuto | 586985
john | 948498
marry | 439483
chichi | 5487584
perter | 954059
cuko | 434893
ketteo | 94394
sukido | 5893458
ruco | 23323
marche | 985948
###############
I want to get user in user.txt and find pass in userpass.txt. The output will be a file with content:
perter | 954059
john | 948498
marry | 439483
chichi | 5487584
marche | 985948
tuto | 586985
ruco | 23323


Please help me solve this requirement. Thanks for your help!
0
Comment
Question by:gpsnemo
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:wilcoxon
ID: 34946362
This should do it...
#!/usr/local/bin/perl

use strict;
use warnings;

my %pass;
open PASS, 'userpass.txt' or die "could not open userpass.txt: $!";
while (<PASS>) {
    chomp;
    my ($u, $p) = split /\s*\|\s*/;
    $pass{$u} = $_;
}
close PASS;

open USER, 'user.txt' or die "could not open user.txt: $!";
while (<USER>) {
    chomp;
    unless (exists $pass{$_}) {
        warn "could not locate user $_ in userpass.txt\n";
        next;
    }
    print $pass{$_}, "\n";
}
close USER;

Open in new window

0
 

Author Comment

by:gpsnemo
ID: 34947871
Hi Wilcoxon,
Thanks for your help,
Have another way beside using hash table to store user and password, example if I have many filed, not only password (the split will be more complicate). Example for user password file:
1 | usa | tuto | 586985
2 | fr | john | 948498
3 | usa | marry | 439483
4 | hk | chichi | 5487584
5 | usa | perter | 954059
6 | usa | cuko | 434893
7 | uk | ketteo | 94394
8 | usa | sukido | 5893458
9 | ca | ruco | 23323
10 | usa | marche | 985948
I hope you will have new solution to solve it, thanks
0
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 34948245
Sure.  This should do what you want and be pretty flexible...

See the comments in the code.
#!/usr/local/bin/perl

use strict;
use warnings;

# 0-based index of username in userpass.txt
my $user_fld = 1;
# 0-based fields to include in output (and order)
# if you wanted to output username, password, country (in that order),
#     it would be (1, 2, 0)
my @fld_order = (1, 2);

my %pass;
open PASS, 'userpass.txt' or die "could not open userpass.txt: $!";
while (<PASS>) {
    chomp;
    my @flds = split /\s*\|\s*/;
    $pass{$flds[$user_fld]} = join('|', @flds[@fld_order]);
}
close PASS;

open USER, 'user.txt' or die "could not open user.txt: $!";
while (<USER>) {
    chomp;
    unless (exists $pass{$_}) {
        warn "could not locate user $_ in userpass.txt\n";
        next;
    }
    print $pass{$_}, "\n";
}
close USER;

Open in new window

0
 

Author Closing Comment

by:gpsnemo
ID: 34948440
It's good for me, thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question