Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Match line number in different file

by ddrew78 (Beadle)
on Jan 06, 2020 at 20:38 UTC ( [id://11111080]=perlquestion: print w/replies, xml ) Need Help??

ddrew78 has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am in need of your wisdom. I have 2 files, that look something like this: File 1
aa-aa1-aa aa-aa2-aa aa-aa2-ab aa-aa3-bc
File 2
V1111:something V1111:something V2222:something V1111:something
A third file will look like this:
aa-aa1-aa aa-aa2-ab
I need to search file 1 for matches to file 3, and then output the corresponding line from file 2. So with the example above, the output should show
V1111:something V2222:something
Essentially, if the first match in file 1 is on line 3, I need line 3 from file 2. I have no idea on how to match those values up to get the output I need. Thanks as always for any ideas.

Replies are listed 'Best First'.
Re: Match line number in different file
by LanX (Saint) on Jan 06, 2020 at 20:42 UTC

    • open file 1 and 2
    • read one line from each and build a $hash{line1}=line2
    • once done open file 3
    • output lines from $hash{line3}
    update

    in case 1 and 2 are very large and 3 is not, then do it the other way round:

    • read file 3 into a %seen hash
    • iterate again simultanously over 1 and 2
    • output lines seen in 3

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Match line number in different file
by tybalt89 (Monsignor) on Jan 06, 2020 at 21:04 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111080 use warnings; my @file1 = split /^/, <<END; aa-aa1-aa aa-aa2-aa aa-aa2-ab aa-aa3-bc END my @file2 = split /^/, <<END; V1111:something V1111:something V2222:something V1111:something END my @file3 = split /^/, <<END; aa-aa1-aa aa-aa2-ab END my %one2two; @one2two{@file1} = @file2; print $one2two{$_} // '' for @file3;
Re: Match line number in different file
by poj (Abbot) on Jan 09, 2020 at 10:13 UTC
    if the first match in file 1

    Are the records in File1 and File3 unique or are there multiple lines with the same value like File2 ?

    poj
      Files 1 and 3 only have unique values.
Re: Match line number in different file
by Anonymous Monk on Jan 09, 2020 at 00:28 UTC
    Ummm ... "How Enormous" are these files?
      These files are not very large, likely no more than about 200 lines.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11111080]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found