in reply to search/grep perl/*nix
I'd have thought that writing to a file and reading it back would have slowed me down, but it didn't!
Without having the time to investigate with a benchmark at the moment, I'd take a guess that most of the time might be being spent on cut | sort | uniq on a 100MB file, not on reading/writing a 50kb temp file or pipe to Perl. The main point I wanted to make is the following:
Is there a bias towards either of these approaches (driven by performance) should the dataset get significantly larger?
I'd have a bias against both of the approaches ;-) All of those tasks can be done in pure Perl, without launching four separate processes. I wrote about the topic of running external processes at length here, but the only advice from there that seems to apply at the moment is "just do it in Perl".
On a simple test file, the following produces the same output as "cut -d"," -f2 /tmp/input.txt | sort | uniq". Note that since I'm locating duplicates with a hash, I don't need to sort the input data first, meaning I can process the file line-by-line without loading all of it into memory. See also How can I remove duplicate elements from a list or array?
use warnings; use strict; my $filename = '/tmp/input.txt'; open my $fh, '<:encoding(UTF-8)', $filename or die "$filename: $!"; my %seen; while (<$fh>) { chomp; my @fields = split /,/; $seen{$fields[1]}++ } close $fh; my @data = sort keys %seen; print $_,"\n" for @data;
If your input file is CSV, you might consider using Text::CSV_XS instead of split, since it will more robustly handle cases like quotes or escaped/quoted separators within fields. Update before posting: 1nickt just showed an example of that. Note the line-by-line approach can also be used with Text::CSV_XS with its getline method (I showed a short example e.g. here).
Update: Added the file encoding from the OP (was it edited?) and minor edits for clarity.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search/grep perl/*nix
by Gtforce (Sexton) on Nov 25, 2017 at 17:17 UTC | |
by haukex (Archbishop) on Nov 25, 2017 at 17:36 UTC | |
by 1nickt (Canon) on Nov 25, 2017 at 17:31 UTC | |
by Laurent_R (Canon) on Nov 25, 2017 at 18:57 UTC | |
by 1nickt (Canon) on Nov 25, 2017 at 20:23 UTC | |
by Anonymous Monk on Nov 25, 2017 at 17:39 UTC |