Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.…
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

12 Experts available now in Live!

Get 1:1 Help Now