in reply to Need to write a file joining data in another two

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.

Replies are listed 'Best First'.
Re^2: Need to write a file joining data in another two
by ssandv (Hermit) on Oct 22, 2010 at 23:05 UTC

    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.