in reply to sorting type question- space problems
G'day baxy77bax,
Without knowing the (average) record size, it's a little hard to estimate whether this solution fits the 100MB memory limit. If the records are of a similar size to your dummy data, then it probably won't meet that requirement; if they are in the order of hundreds bytes each, then you may be in with a chance.
Anyway, here's the potential solution [Updated - see below]:
$ perl -Mstrict -Mwarnings -Mautodie -le ' use Tie::File; tie my @input_records, "Tie::File", "./pm_1054101_input.txt"; open my $out_fh, ">", "./pm_1054101_output.txt"; print $out_fh $input_records[$_] for map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } map { [ $_, map { substr $_, 3 } (split " ", $input_records[$_ +])[0,1] ] } 0 .. $#input_records; close $out_fh; untie @input_records; '
Input file:
$ cat pm_1054101_input.txt key1 key2 ndnjfgdsjfjjkjjfjf... key1 key2 kdfkjdfgdfugbjndkfgkjgndkjfjkd key43 key21 sdkjfhdghdbgbd key1 key3 jujdejnsduhffnjj key2 key2 jhzezhdjjf...
Output file:
$ cat pm_1054101_output.txt key1 key2 ndnjfgdsjfjjkjjfjf... key1 key2 kdfkjdfgdfugbjndkfgkjgndkjfjkd key1 key3 jujdejnsduhffnjj key2 key2 jhzezhdjjf... key43 key21 sdkjfhdghdbgbd
File sizes unchanged:
$ ls -l pm_1054101_* -rw-r--r-- 1 ken staff 159 14 Sep 22:41 pm_1054101_input.txt -rw-r--r-- 1 ken staff 159 14 Sep 23:04 pm_1054101_output.txt
Update: I removed an intermediary array which might save some memory. Here's the original code:
$ perl -Mstrict -Mwarnings -Mautodie -le ' use Tie::File; tie my @input_records, "Tie::File", "./pm_1054101_input.txt"; my @sorted_record_numbers = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } map { [ $_, map { substr $_, 3 } (split " ", $input_records[ +$_])[0,1] ] } 0 .. $#input_records; open my $out_fh, ">", "./pm_1054101_output.txt"; for (@sorted_record_numbers) { print $out_fh $input_records[$_]; } close $out_fh; untie @input_records; '
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sorting type question- space problems
by BrowserUk (Patriarch) on Sep 14, 2013 at 14:27 UTC | |
by kcott (Archbishop) on Sep 15, 2013 at 03:36 UTC | |
by BrowserUk (Patriarch) on Sep 15, 2013 at 11:48 UTC | |
by kcott (Archbishop) on Sep 16, 2013 at 03:28 UTC | |
|
Re^2: sorting type question- space problems
by baxy77bax (Deacon) on Sep 14, 2013 at 13:50 UTC | |
by mbethke (Hermit) on Sep 14, 2013 at 19:42 UTC | |
by BrowserUk (Patriarch) on Sep 15, 2013 at 13:55 UTC |