http://www.perlmonks.org?node_id=79803


in reply to Search and replace on a 130000+ line file.

Here is my quick and nasty solution. This loads the first file into a hash so it may use up lots of memory. Though if it too is 130,000 lines that is only a few megabytes.

Should run fast! (Tested also ;-)

use strict; die "Syntax: $0 <names> < djb-file > output-file" unless @ARGV == 1; my %name_to_ip; open(IN, "<".shift) or die; while (<IN>) { chomp; my ($ip, $name) = split /:/; $name_to_ip{$ip} = $name; } close IN or die; while (<>) { chomp; my ($name, $ip) = split /:/; print $name_to_ip{$ip} || $name, ":", $ip, "\n"; }

Replies are listed 'Best First'.
Re: Re: Search and replace on a 130000+ line file.
by sierrathedog04 (Hermit) on May 11, 2001 at 23:05 UTC
    For whatever reason, the line:
    open(IN, "<".shift) or die;
    doesn't work for me in ActiveState Perl 5.6 /NT workstation when I call the above program as
    perl tempo.pl ip.dat djb.dat

    However, the program works great when I replace that line with:

    open(IN, "<ip.dat") or die;
    and call it as:
    perl tempo.pl djb.dat