Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-21
4
Medium Priority
?
267 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

670 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