in reply to Need to write a file joining data in another two
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 | |
by kennethk (Abbot) on Oct 22, 2010 at 23:13 UTC |