Debian Codegraph @

examples/csvdiff

raw
1 #!/pro/bin/perl
2
3 use 5.012000;
4 use warnings;
5
6 # csvdiff: Show differences between CSV files
7 # (m)'25 [13 Nov 2025] Copyright H.M.Brand 2009-2026
8
9 our $VERSION = "1.04 - 20251113";
10
11 sub usage {
12 my $err = shift and select STDERR;
13 print "usage: csvdiff [--no-color] [--html] [-w|-b|-Z] file.csv file.csv\n",
14 " provides colorized diff on sorted CSV files\n",
15 " assuming first line is header and first field is the key\n",
16 " --no-color do not use colors\n",
17 " -h --html produce HTML output\n",
18 " -w --ignore-all-space ignore all white space\n",
19 " -b --ignore-space-change ignore changes in the amount of white space\n",
20 " -Z --ignore-trailing-space ignore white space at line end\n",
21 " -o F --output=F send output to file F\n";
22 exit $err;
23 } # usage
24
25 use Getopt::Long qw(:config bundling nopermute );
26 my $opt_c = !$ENV{NO_COLOR};
27 GetOptions (
28 "help|?" => sub { usage (0); },
29 "V|version" => sub { print "csvdiff [$VERSION]\n"; exit 0 },
30
31 "w|ignore-all-space!" => \my $opt_w,
32 "b|ignore-ws|ignore-space-change!" => \my $opt_b,
33 "Z|ignore-trailing-space!" => \my $opt_Z,
34
35 "c|color|colour!" => \ $opt_c,
36 "h|html" => \my $opt_h,
37
38 "o|output=s" => \my $opt_o,
39 ) or usage (1);
40
41 @ARGV == 2 or usage (1);
42
43 if ($opt_o) {
44 open STDOUT, ">", $opt_o or die "$opt_o: $!\n";
45 }
46 $| = 1;
47
48 use HTML::Entities;
49 use Term::ANSIColor qw(:constants);
50 use Text::CSV_XS;
51 my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 0 });
52
53 if ($opt_h) {
54 binmode STDOUT, ":encoding(utf-8)";
55 my $name = $^O eq "MSWin32" ? Win32::LoginName () : scalar getpwuid $<;
56 print <<"EOH";
57 <?xml version="1.0" encoding="utf-8"?>
58 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
59 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
60 <head>
61 <title>CFI School updates</title>
62 <meta name="Generator" content="perl $]" />
63 <meta name="Author" content="$name" />
64 <meta name="Description" content="CSV diff @ARGV" />
65 <style type="text/css">
66 .rd { background: #ffe0e0; }
67 .gr { background: #e0ffe0; }
68 .hd { background: #e0e0ff; }
69 .b0 { background: #e0e0e0; }
70 .b1 { background: #f0f0f0; }
71 .r { color: red; }
72 .g { color: green; }
73 </style>
74 </head>
75 <body>
76
77 <h1>CSV diff @ARGV</h1>
78
79 <table>
80 EOH
81 $::{RED} = sub { "\cA\rr"; };
82 $::{GREEN} = sub { "\cA\rg"; };
83 $::{RESET} = sub { ""; };
84 }
85 elsif (!$opt_c) {
86 $::{$_} = sub { "" } for qw( RED GREEN RESET );
87 }
88
89 my @f;
90 my $opt_n = 1;
91 foreach my $x (0, 1) {
92 open my $fh, "<", $ARGV[$x] or die "$ARGV[$x]: $!\n";
93 my $n = 0;
94 while (1) {
95 my $row = $csv->getline ($fh) or last;
96 @$row and push @{$f[$x]}, $row;
97 $n++ && $row->[0] =~ m/\D/ and $opt_n = 0;
98 }
99 }
100 my @n = map { $#{$f[$_]} } 0, 1;
101 my @i = (1, 1);
102 my $hdr = "# csvdiff < $ARGV[0] > $ARGV[1]\n";
103
104 $f[$_][1+$n[$_]][0] = $opt_n ? 0x7FFFFFFF : "\xff\xff\xff\xff" for 0, 1;
105
106 my %cls;
107 %cls = (
108 "b" => 0,
109 "-" => sub { "rd" },
110 "+" => sub { "gr" },
111 "H" => sub { "hd" },
112 "<" => sub { $cls{b} ^= 1; "b$cls{b}" },
113 ">" => sub { "b$cls{b}" },
114 );
115
116 sub show {
117 my ($pfx, $x) = @_;
118 my $row = $f[$x][$i[$x]++] or return;
119
120 if ($opt_h) {
121 my $bg = $cls{$pfx}->();
122 print qq{ <tr class="$bg">},
123 (map{"<td".(s/^\cA\r([gr])//?qq{ class="$1"}:"").">$_</td>"}@$row),
124 "</tr>\n";
125 return;
126 }
127
128 print $hdr, $pfx, " ", $pfx eq "-" ? RED : $pfx eq "+" ? GREEN : "";
129 $csv->print (*STDOUT, $row);
130 print RESET, "\n";
131 $hdr = "";
132 } # show
133
134 # Skip first line of both are same: it probably is a header
135 my @h0 = @{$f[0][0]};
136 my @h1 = @{$f[1][0]};
137 if ("@h0" eq "@h1") {
138 if ($opt_h) {
139 $i[0]--;
140 show ("H", 0);
141 }
142 shift @{$f[0]};
143 shift @{$f[1]};
144 }
145
146 my $x = 0;
147 ROW: while ($i[0] <= $n[0] || $i[1] <= $n[1]) {
148 my @r0 = @{$f[0][$i[0]]};
149 my @r1 = @{$f[1][$i[1]]};
150
151 if ($opt_n) {
152 $r0[0] < $r1[0] and show ("-", 0), next;
153 $r0[0] > $r1[0] and show ("+", 1), next;
154 }
155 else {
156 $r0[0] lt $r1[0] and show ("-", 0), next;
157 $r0[0] gt $r1[0] and show ("+", 1), next;
158 }
159
160 my @v0 = @r0;
161 my @v1 = @r1;
162 $opt_Z and s/[\r\n\s]+\z// for @v0, @v1;
163 $opt_b and s/[\r\n\s]+/ /g for @v0, @v1;
164 $opt_w and s/[\r\n\s]+//g for @v0, @v1;
165
166 { local $" = "\x{ff}";
167 if ("@v0" eq "@v1") {
168 $i[0]++;
169 $i[1]++;
170 next ROW;
171 }
172 }
173
174 foreach my $c (1 .. $#h0) {
175 my ($L, $R) = map { $_ // "" } $r0[$c], $r1[$c];
176 $L eq $R and next;
177 $f[0][$i[0]][$c] = RED . $L . RESET;
178 $f[1][$i[1]][$c] = GREEN . $R . RESET;
179 }
180
181 show ("<", 0);
182 show (">", 1);
183 }
184
185 $opt_h and print " </table>\n</body>\n</html>\n";
186
187 close STDOUT;