Solved

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

Posted on 2011-02-21
4
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

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 26

Accepted Solution

by:
wilcoxon earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

624 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