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.


In reply to Re: Need to write a file joining data in another two by kennethk
in thread Need to write a file joining data in another two by ranrodrig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.