Solved

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

Posted on 2011-02-21
4
225 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 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now