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

Folks I need your help cuz I've a file with 100,000 records that need to be compared against a passwd file (300) and then create a third one with the data in the first one and the passwd from the second one set in it.
The format of the first file is:

host xxxxxx "" 0,0 Closed control00/ SOLARIS 0x0+0+0 "TCP"
...
host 222222 "" 0,0 Closed control30/ Linux 0x0+0+0 "TCP"

The second one has a filed tha acts as a key and the passwd as below:

control00 45EF0E4228F59D60521D071AC55E4BAC
...
control30 F93F0F0BC36E597B28D325BD10F99C4B

The third file should looks like:

host 222222 "" 0,0 Closed control30/F93F0F0BC36E597B28D325BD10F99C4B Linux 0x0+0+0 "TCP"

Now you migth think piece of cake for you yes, for me no, I know that I need to read one record in the second file and the read all the records in the first one with this passwd in it and then generate the third and so on, but to be honest I don't know how to open, read & control each of them in perl.

I already reviewed the archive with no luck cuz, there're only examples about comparing files.

Can you help me with this?, I'm lost and desperate.

TIA for your help. Raul

  • Comment on Need to write a file joining data in another two

Replies are listed 'Best First'.
Re: Need to write a file joining data in another two
by kennethk (Abbot) on Oct 22, 2010 at 14:53 UTC
    This can be done very simply, but you run the risk of making unintended modifications since I don't know how well-constrained your input text is. Can you guarantee that your password keys never contain whitespace? Are the whitespace patterns in the larger file identical on every line? Can you guarantee that password keys never collide with any other part of the line? Without detailed knowledge of your precise inputs, any solution is quite likely to be buggy and most likely suboptimal.

    Given that your password file is fairly small, I would suggest reading in the entire file and storing the results in a hash. You could then go over your second file, line by line to save memory, and sub in the passwords with a regular expression. Something like:

    #!/usr/bin/perl use strict; use warnings; open my $pass_handle, '<', 'passwords.txt' or die "Open fail: $!"; my %password; while (<$pass_handle>) { chomp; my ($key,$pass) = split; $password{$key} = $pass; } open my $input_handle, '<', 'input.txt' or die "Open fail: $!"; open my $output_handle, '>', 'output.txt' or die "Open fail: $!"; while (<$input_handle>) { s/^(\S+\s+\S+\s+".*"\s+\S+\s+\S+\s+)(\S+)(\/)/$1$2$3$password{$2}/ +; print $output_handle $_; }

    That regular expression is likely much uglier than it needs to be, but I really have no idea how your actual input is formatted. Note that I use the special variable $_ extensively.

      That might not do the right thing if $2 isn't a key in the hash, also.

        True, but it will do no harm and will emit a warning (unitialized value) with the correct input file line number, which is usually as much as you can hope for in this case.
Re: Need to write a file joining data in another two
by Anonymous Monk on Oct 22, 2010 at 14:52 UTC
    Can you help me with this?, I'm lost and desperate.

    Baww, i weep for you, must be really hard to keep that going since march