-
Notifications
You must be signed in to change notification settings - Fork 2
/
clean_mapping
executable file
·132 lines (103 loc) · 3.77 KB
/
clean_mapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env perl -w
use strict;
use Text::CSV;
use DateTime;
use FileHandle;
use Getopt::Long;
use Pod::Usage;
use Carp;
my ($IN_SEPARATOR, $OUT_SEPARATOR) = (',', ',');
my ($HELP, $MAN, $KEEP);
my $JOIN_STR;
my @JOIN = ();
GetOptions(
'help|?' => \$HELP,
'man' => \$MAN,
'keep' => \$KEEP,
'join=s' => \$JOIN_STR,
'in-separator=s' => \$IN_SEPARATOR,
'out-separator=s' => \$OUT_SEPARATOR
);
@JOIN = split /,/, $JOIN_STR if $JOIN_STR;
pod2usage(-exitstatus => 0, -verbose => 1) if $HELP;
pod2usage(-exitstatus => 0, -verbose => 2) if $MAN;
my ($SUPERSET_KEY, $SUPERSET_FILE, $SUBSET_KEY, $SUBSET_FILE) = @ARGV;
pod2usage(-exitstatus => 1, -verbose => 1) unless ($SUBSET_KEY);
my $CSV_IN = Text::CSV->new({ binary => 1, sep_char => $IN_SEPARATOR });
my $CSV_OUT = Text::CSV->new({ binary => 1, sep_char => $OUT_SEPARATOR });
sub main() {
my %superset_keys = ();
process_csv($SUPERSET_FILE, \&build_superset_keys,
[ $SUPERSET_KEY, \%superset_keys ]);
process_csv($SUBSET_FILE, \&dump_valid_line,
[ $SUBSET_KEY, \%superset_keys ], 1);
}
sub process_csv($$$;$) {
my ($file, $sub, $params, $dump_headers) = @_;
my $fh = $file
? new FileHandle($file, 'r')
: *STDIN;
croak "Cannot open '$file' for reading: $!" unless $fh;
my @headers = @{ $CSV_IN->getline($fh) or croak "Cannot parse header line: $!" };
if ($dump_headers) {
my @out = @headers;
push @out, @JOIN;
$CSV_OUT->combine(@out);
print $CSV_OUT->string . "\n";
}
$CSV_IN->column_names(@headers); # Set column names for getline_hr ()
until ($CSV_IN->eof) {
my $hash = $CSV_IN->getline_hr($fh) or next;
$sub->(\@headers, $hash, @$params);
}
close $fh if $file;
}
sub build_superset_keys($$$$) {
my ($headers, $hash, $key, $keys) = @_;
$keys->{$hash->{$key}} = $hash;
}
sub dump_valid_line($$$$$) {
my ($headers, $hash, $key, $sup_keys) = @_;
my $sup = $sup_keys->{$hash->{$key}};
my @out;
if (defined $sup) {
@out = map { $hash->{$_} } @$headers;
push @out, map { $sup->{$_} } @JOIN;
$CSV_OUT->combine(@out);
print $CSV_OUT->string . "\n";
} elsif ($KEEP) {
@out = map { ($_ eq $SUBSET_KEY) ? '' : $hash->{$_} } @$headers;
push @out, map { '' } @JOIN;
$CSV_OUT->combine(@out);
print $CSV_OUT->string . "\n";
}
}
main();
__END__
=head1 NAME
clean_mapping - referencial integrity cleanup tool. Filters out lines that
violate the specified referencial integrity constrain.
=head1 SYNOPSIS
clean_mapping [options] pk_column_name referenced_csv_file \
fk_column_name processed_csv_file
clean_mapping [options] pk_column_name referenced_csv_file \
fk_column_name < processed_csv_file
Example:
#
# filter out rows of salaries.csv that contain an 'Employee ID' value with
# no matching value of 'ID' in employees.csv
#
check_mapping salaries.csv 'Employee ID' employees.csv 'ID'
=head1 OPTIONS
--help, -h short help screen
--man slightly more detailed documentation
--keep keeps the violating lines but replaces offending
foreign keys with blanks
--join=list comma separated list of the fields from the referenced
CSV file to be added to output stream. An equivalent
of performing an inner join (or an outer join if the
--keep switch is used)
--in-separator=char field separator in the source file ("," by default)
--out-separator=char output field separator ("," by default)
=head1 AUTHORS
Pavel Kolesnikov <[email protected]>