| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use Modern::Perl; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use autodie qw(:all); | ||
| 7 | no indirect ':fatal'; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | |||
| 11 | use Ascii; | ||
| 12 | use Text::CSV ; | ||
| 13 | use Readonly ; | ||
| 14 | use List::MoreUtils qw(any) ; | ||
| 15 | |||
| 16 | use File::Basename; | ||
| 17 | use Smart::Comments; | ||
| 18 | use Try::Tiny; | ||
| 19 | |||
| 20 | use Getopt::Euclid qw( :vars<opt_> ); | ||
| 21 | use List::MoreUtils qw{firstidx} ; | ||
| 22 | use Data::Dumper; | ||
| 23 | use Regexp::Exhaustive qw/ exhaustive /; | ||
| 24 | |||
| 25 | use version ; our $VERSION = qv('1.0.0'); | ||
| 26 | our $opt_asciifile; | ||
| 27 | our $opt_csvfile; | ||
| 28 | our $opt_linenumber; | ||
| 29 | our $opt_nodebug; | ||
| 30 | |||
| 31 | use Text::CSV; | ||
| 32 | |||
| 33 | my $csvfile = $opt_csvfile; | ||
| 34 | my $asciifile = $opt_asciifile; | ||
| 35 | my $linenumber = $opt_linenumber; | ||
| 36 | my $debug = !$opt_nodebug; | ||
| 37 | |||
| 38 | my $li=0; | ||
| 39 | |||
| 40 | my %csv_row; | ||
| 41 | my $ascii_row; | ||
| 42 | |||
| 43 | my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 }); | ||
| 44 | open my $fh, "<:encoding(utf8)", $csvfile or die "csvfile: $!"; | ||
| 45 | $csv->header($fh); | ||
| 46 | while ((my $row = $csv->getline_hr ($fh)) && ($li++ < $linenumber)) { | ||
| 47 | # warn Dumper $row; | ||
| 48 | %csv_row = %$row; | ||
| 49 | } | ||
| 50 | #warn Dumper %csv_row; | ||
| 51 | #warn Dumper keys %csv_row; | ||
| 52 | my @vals = sort { length($csv_row{$b}) <=> length($csv_row{$a}) } keys %csv_row; | ||
| 53 | |||
| 54 | close $fh; | ||
| 55 | |||
| 56 | |||
| 57 | $li=0; | ||
| 58 | open(my $afile, "<", $asciifile) or die "Could not open file $!"; | ||
| 59 | #skip header | ||
| 60 | my $row = <$afile>; | ||
| 61 | while (defined(my $row = <$afile> ) && ($li++ < $linenumber)) { | ||
| 62 | chomp $row; | ||
| 63 | $ascii_row = $row; | ||
| 64 | } | ||
| 65 | #warn Dumper $ascii_row; | ||
| 66 | #print "done\n"; | ||
| 67 | |||
| 68 | #find where vals in csv match ascii | ||
| 69 | |||
| 70 | #say $ascii_row; | ||
| 71 | my %positions ; | ||
| 72 | foreach my $v (@vals){ | ||
| 73 | my $val = $csv_row{$v}." *"; | ||
| 74 | # warn Dumper $val; | ||
| 75 | |||
| 76 | $ascii_row =~ m/(?>$val)/; | ||
| 77 | $positions{$v} = [@-,@+]; | ||
| 78 | #warn Dumper @-, @+; | ||
| 79 | |||
| 80 | $ascii_row = $`. "^"x length($&) . $'; | ||
| 81 | #say $ascii_row; | ||
| 82 | # warn Dumper $$_[0][0],$$_[1][0],$$_[2][0],$$_[3],$$_[4] for exhaustive($ascii_row => qr/(?>$val)/, qw[ @- $^R @+ $` $']); | ||
| 83 | } | ||
| 84 | |||
| 85 | my @sortedkeys = sort { $positions{$a}[0] <=> $positions{$b}[0]} keys %positions; | ||
| 86 | { | ||
| 87 | my $i=1; | ||
| 88 | foreach my $k (@sortedkeys) { | ||
| 89 | if ($debug) { say $k,"\t"x((55-length($k))/8), $positions{$k}[0],"\t", $positions{$k}[1]}; | ||
| 90 | $i++; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | if (!$debug) {say "12.0"}; | ||
| 94 | if (!$debug) {say scalar(@sortedkeys)}; | ||
| 95 | { | ||
| 96 | my $i=1; | ||
| 97 | foreach my $k (@sortedkeys) { | ||
| 98 | if (!$debug) {say $i,"\t","SQLCHAR","\t","0","\t",$positions{$k}[1]-$positions{$k}[0],"\t",($i == scalar(@sortedkeys)) ? '"\r\n"':'""',"\t", $i,"\t", $k,"\t"x((55-length($k))/8),"SQL_Latin1_general_CP1_CI_AS"}; | ||
| 99 | $i++; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | #warn Dumper %positions ; | ||
| 104 | #foreach my $v (@val; | ||
| 105 | |||
| 106 | #my $topval = $csv_row{$vals[1]}." *"; | ||
| 107 | #warn $topval; | ||
| 108 | |||
| 109 | #warn Dumper $$_[0][0],$$_[1][0],$$_[2][0],$$_[3],$$_[4], $$_[5] for exhaustive($ascii_row => qr/(?>$topval)/, qw[ @- $^R @+ $` $& $']); | ||
| 110 | |||
| 111 | #warn Dumper $_ for exhaustive($ascii_row => qr/(?>$topval)/, qw[ @- $^R @+ $` $' $&]); | ||
| 112 | |||
| 113 | ## and write as CSV | ||
| 114 | #open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!"; | ||
| 115 | #$csv->say ($fh, $_) for @rows; | ||
| 116 | #close $fh or die "new.csv: $!"; | ||
| 117 | |||
| 118 | |||
| 119 | exit ; | ||
| 120 | |||
| 121 | # ####################################################################################### | ||
| 122 | |||
| 123 | |||
| 124 | END { | ||
| 125 | } | ||
| 126 | |||
| 127 | __DATA__ |