PERL -pe "s/\x22//g" output.csv > output2.csv
####
use Text::CSV;
my ($in, $out, $csvi, $csvo, $ar, $col, $hri, $hro, $yyyy, $mm, $dd, $code, $desc);
my @hdr = qw(DeviceID PosTxnID MobileNumber Timestamp PosID PosUserID ShopID
Prodcode ProdDescription ProdDept ProdGroup1 ProdGroup2 Qty Value);
my @e14 = ('','','','','','','','','','','','','',''); my $spacer = 0;
# open $in, "new( { binary=>1 } );
open $out, ">output.csv" or die "output.csv: $!\n";
$csvo = Text::CSV->new( { binary=>1, eol=>$/ } );
$ar = $csvi->getline($in); $col = 0;
foreach (@{$ar}) { $_ = "Col$col" if $col>7; s/ //; $col++; }
$csvi->column_names($ar); $csvo->column_names(@hdr); $csvo->print($out,\@hdr);
while ($hri = $csvi->getline_hr($in)) {
if ($hri->{InvDate}) {
($dd,$mm,$yyyy) = split("/",$hri->{InvDate});
$hro = { DeviceID=>$hri->{CustCode}, PosTxnID=>$hri->{InvNum}, MobileNumber=>'',
Timestamp=>$yyyy.'-'.$mm.'-'.$dd.' 00:00', PosID=>'', PosUserID=>'',
ShopID=>'2345', Prodcode=>$code, ProdDescription=>$desc, ProdDept=>'',
ProdGroup1=>'', ProdGroup2=>'', Qty=>$hri->{Qty}, Value=>$hri->{NetAmt} };
if ($spacer) { $csvo->print($out,\@e14); $spacer = 0; }
$csvo->print_hr($out,$hro);
}
elsif ($hri->{CustCode}) { ($code,$desc) = split(' - # ',$hri->{CustCode}); }
elsif ($hri->{LineDisc}) { $spacer = 0; }
}
# original code had { $spacer = 1; } I changed this as my input.csv file originally had a - but now contains - # between the Product code and product description in the input.csv file
####
"Cust Code","Inv Date","Inv Num","Type","Unit Price","Line Disc","Qty","Net Amt","Cost Amt","Margin","%","Market Code","",
"ZB101 - # Acme Widget Large 20 inch","","","","","","","","","","","","",
"VINT01","23/01/2018",112681,"I",23.50,"",1.00,23.50,"",23.50,"100.00%","DEFAULT","",
"RR01","23/01/2018",112683,"I",23.50,"",2.00,47.00,"",47.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",3.00,70.50,"",70.50,"100.00%","","",
"","","","","","","","","","","","","",
"ZB1201 - # Acme Widget Small 5 inch","","","","","","","","","","","","",
"VINT01","23/01/2018",112681,"I",50.00,"",2.00,100.00,"",100.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",2.00,100.00,"",100.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB2101 - # Acme Widget Large 15 inch","","","","","","","","","","","","",
"PIP01","23/01/2018",112670,"I",12.50,"",1.00,12.50,"",12.50,"100.00%","DEFAULT","",
"VINT01","23/01/2018",112681,"I",12.50,"",1.00,12.50,"",12.50,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",2.00,25.00,"",25.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB2401 - # Acme Widget Medium 10 inch","","","","","","","","","","","","",
"VCR01","23/01/2018",112674,"I",32.50,"",2.00,65.00,"",65.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",2.00,65.00,"",65.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB2501 - # Acme Widget Small 6 inch","","","","","","","","","","","","",
"VINT01","23/01/2018",112681,"I",37.50,"",1.00,37.50,"",37.50,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",1.00,37.50,"",37.50,"100.00%","","",
"","","","","","","","","","","","","",
"ZB3501 - # Acme Widget GOLD","","","","","","","","","","","","",
"CAPB01","23/01/2018",112672,"I",29.00,"",1.00,29.00,"",29.00,"100.00%","DEFAULT","",
"SANML","23/01/2018",112673,"I",29.00,"",1.00,29.00,"",29.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",2.00,58.00,"",58.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB3701 - # Acme Widget Large 24 inch","","","","","","","","","","","","",
"RS01","23/01/2018",112657,"I",21.00,"",1.00,21.00,"",21.00,"100.00%","DEFAULT","",
"CAPB01","23/01/2018",112672,"I",21.00,"",1.00,21.00,"",21.00,"100.00%","DEFAULT","",
"BPS01","23/01/2018",112679,"I",21.00,"",2.00,42.00,"",42.00,"100.00%","DEFAULT","",
"VINT01","23/01/2018",112681,"I",21.00,"",1.00,21.00,"",21.00,"100.00%","DEFAULT","",
"MC01","23/01/2018",112682,"I",21.00,"",1.00,21.00,"",21.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",6.00,126.00,"",126.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB3801 - # Regular Acme Widget Large 20 inch","","","","","","","","","","","","",
"PIP01","23/01/2018",112670,"I",21.00,"",1.00,21.00,"",21.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",1.00,21.00,"",21.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB4001 - # High Type Acme Widget","","","","","","","","","","","","",
"RE01","23/01/2018",112663,"I",30.00,"",1.00,30.00,"",30.00,"100.00%","DEFAULT","",
"CAPB01","23/01/2018",112672,"I",30.00,"",1.00,30.00,"",30.00,"100.00%","DEFAULT","",
"APP01","23/01/2018",112685,"I",30.00,"",1.00,30.00,"",30.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",3.00,90.00,"",90.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB401 - # Nugget Type Acme Widget","","","","","","","","","","","","",
"RR01","23/01/2018",112683,"I",24.00,"",2.00,48.00,"",48.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",2.00,48.00,"",48.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB4901 - # Acme Widget Large 17 inch","","","","","","","","","","","","",
"TB01","23/01/2018",112665,"I",38.50,"",1.00,38.50,"",38.50,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",1.00,38.50,"",38.50,"100.00%","","",
"","","","","","","","","","","","","",
"ZB501 - # Acme Widget hexangle","","","","","","","","","","","","",
"DD01","23/01/2018",112659,"I",33.00,"",3.00,99.00,"",99.00,"100.00%","DEFAULT","",
"VINT01","23/01/2018",112681,"I",33.00,"",1.00,33.00,"",33.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",4.00,132.00,"",132.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB701 - # Acme Widget Circular 20 inch","","","","","","","","","","","","",
"RS01","23/01/2018",112657,"I",33.00,"",1.00,33.00,"",33.00,"100.00%","DEFAULT","",
"PIP01","23/01/2018",112670,"I",33.00,"",1.00,33.00,"",33.00,"100.00%","DEFAULT","",
"BPS01","23/01/2018",112679,"I",33.00,"",1.00,33.00,"",33.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",3.00,99.00,"",99.00,"100.00%","","",
"","","","","","","","","","","","","",
"ZB901 - # Acme Widget Square 3 inch","","","","","","","","","","","","",
"RE01","23/01/2018",112663,"I",32.00,"",1.00,32.00,"",32.00,"100.00%","DEFAULT","",
"TB01","23/01/2018",112665,"I",32.00,"",1.00,32.00,"",32.00,"100.00%","DEFAULT","",
"","","","","","","––––––","–––––––––","–––––––––","–––––––––","–––––––","","",
"","","","","","Product Totals:",2.00,64.00,"",64.00,"100.00%","","",
"","","","","","","","","","","","","",
####
DeviceID,PosTxnID,MobileNumber,Timestamp,PosID,PosUserID,ShopID,Prodcode,ProdDescription,ProdDept,ProdGroup1,ProdGroup2,Quantity,Value
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,
####
,,,,,,––––––,–––––––––,–––––––––,–––––––––,–––––––,<
,,,,,Product Totals:,3,70.5,,70.5,100.00%,
,,,,,,,,,,,
And
,,,,,,––––––,–––––––––,–––––––––,–––––––––,–––––––,
,,,,,Product Totals:,2,100,,100,100.00%,
,,,,,,,,,,,
####
DeviceID,PosTxnID,MobileNumber,Timestamp,PosID,PosUserID,ShopID,Prodcode,ProdDescription,ProdDept,ProdGroup1,ProdGroup2,Qty,Value
VINT01,112681,,"2018-01-23 00:00",,,2345,ZB101,"Acme Widget Large 20 inch",,,,1.00,23.50
RR01,112683,,"2018-01-23 00:00",,,2345,ZB101,"Acme Widget Large 20 inch",,,,2.00,47.00
VINT01,112681,,"2018-01-23 00:00",,,2345,ZB1201,"Acme Widget Small 5 inch",,,,2.00,100.00
PIP01,112670,,"2018-01-23 00:00",,,2345,ZB2101,"Acme Widget Large 15 inch",,,,1.00,12.50
VINT01,112681,,"2018-01-23 00:00",,,2345,ZB2101,"Acme Widget Large 15 inch",,,,1.00,12.50
VCR01,112674,,"2018-01-23 00:00",,,2345,ZB2401,"Acme Widget Medium 10 inch",,,,2.00,65.00
VINT01,112681,,"2018-01-23 00:00",,,2345,ZB2501,"Acme Widget Small 6 inch",,,,1.00,37.50
CAPB01,112672,,"2018-01-23 00:00",,,2345,ZB3501,"Acme Widget GOLD",,,,1.00,29.00
SANML,112673,,"2018-01-23 00:00",,,2345,ZB3501,"Acme Widget GOLD",,,,1.00,29.00
RS01,112657,,"2018-01-23 00:00",,,2345,ZB3701,"Acme Widget Large 24 inch",,,,1.00,21.00
CAPB01,112672,,"2018-01-23 00:00",,,2345,ZB3701,"Acme Widget Large 24 inch",,,,1.00,21.00
BPS01,112679,,"2018-01-23 00:00",,,2345,ZB3701,"Acme Widget Large 24 inch",,,,2.00,42.00
VINT01,112681,,"2018-01-23 00:00",,,2345,ZB3701,"Acme Widget Large 24 inch",,,,1.00,21.00
MC01,112682,,"2018-01-23 00:00",,,2345,ZB3701,"Acme Widget Large 24 inch",,,,1.00,21.00
PIP01,112670,,"2018-01-23 00:00",,,2345,ZB3801,"Regular Acme Widget Large 20 inch",,,,1.00,21.00
RE01,112663,,"2018-01-23 00:00",,,2345,ZB4001,"High Type Acme Widget",,,,1.00,30.00
CAPB01,112672,,"2018-01-23 00:00",,,2345,ZB4001,"High Type Acme Widget",,,,1.00,30.00
APP01,112685,,"2018-01-23 00:00",,,2345,ZB4001,"High Type Acme Widget",,,,1.00,30.00
RR01,112683,,"2018-01-23 00:00",,,2345,ZB401,"Nugget Type Acme Widget",,,,2.00,48.00
TB01,112665,,"2018-01-23 00:00",,,2345,ZB4901,"Acme Widget Large 17 inch",,,,1.00,38.50
DD01,112659,,"2018-01-23 00:00",,,2345,ZB501,"Acme Widget hexangle",,,,3.00,99.00
VINT01,112681,,"2018-01-23 00:00",,,2345,ZB501,"Acme Widget hexangle",,,,1.00,33.00
RS01,112657,,"2018-01-23 00:00",,,2345,ZB701,"Acme Widget Circular 20 inch",,,,1.00,33.00
PIP01,112670,,"2018-01-23 00:00",,,2345,ZB701,"Acme Widget Circular 20 inch",,,,1.00,33.00
BPS01,112679,,"2018-01-23 00:00",,,2345,ZB701,"Acme Widget Circular 20 inch",,,,1.00,33.00
RE01,112663,,"2018-01-23 00:00",,,2345,ZB901,"Acme Widget Square 3 inch",,,,1.00,32.00
TB01,112665,,"2018-01-23 00:00",,,2345,ZB901,"Acme Widget Square 3 inch",,,,1.00,32.00
####
DeviceID,PosTxnID,MobileNumber,Timestamp,PosID,PosUserID,ShopID,Prodcode,ProdDescription,ProdDept,ProdGroup1,ProdGroup2,Qty,Value
VINT01,112681,,2018-01-23 00:00,,,2345,ZB101,Acme Widget Large 20 inch,,,,1.00,23.50
RR01,112683,,2018-01-23 00:00,,,2345,ZB101,Acme Widget Large 20 inch,,,,2.00,47.00
VINT01,112681,,2018-01-23 00:00,,,2345,ZB1201,Acme Widget Small 5 inch,,,,2.00,100.00
PIP01,112670,,2018-01-23 00:00,,,2345,ZB2101,Acme Widget Large 15 inch,,,,1.00,12.50
VINT01,112681,,2018-01-23 00:00,,,2345,ZB2101,Acme Widget Large 15 inch,,,,1.00,12.50
VCR01,112674,,2018-01-23 00:00,,,2345,ZB2401,Acme Widget Medium 10 inch,,,,2.00,65.00
VINT01,112681,,2018-01-23 00:00,,,2345,ZB2501,Acme Widget Small 6 inch,,,,1.00,37.50
CAPB01,112672,,2018-01-23 00:00,,,2345,ZB3501,Acme Widget GOLD,,,,1.00,29.00
SANML,112673,,2018-01-23 00:00,,,2345,ZB3501,Acme Widget GOLD,,,,1.00,29.00
RS01,112657,,2018-01-23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch,,,,1.00,21.00
CAPB01,112672,,2018-01-23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch,,,,1.00,21.00
BPS01,112679,,2018-01-23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch,,,,2.00,42.00
VINT01,112681,,2018-01-23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch,,,,1.00,21.00
MC01,112682,,2018-01-23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch,,,,1.00,21.00
PIP01,112670,,2018-01-23 00:00,,,2345,ZB3801,Regular Acme Widget Large 20 inch,,,,1.00,21.00
RE01,112663,,2018-01-23 00:00,,,2345,ZB4001,High Type Acme Widget,,,,1.00,30.00
CAPB01,112672,,2018-01-23 00:00,,,2345,ZB4001,High Type Acme Widget,,,,1.00,30.00
APP01,112685,,2018-01-23 00:00,,,2345,ZB4001,High Type Acme Widget,,,,1.00,30.00
RR01,112683,,2018-01-23 00:00,,,2345,ZB401,Nugget Type Acme Widget,,,,2.00,48.00
TB01,112665,,2018-01-23 00:00,,,2345,ZB4901,Acme Widget Large 17 inch,,,,1.00,38.50
DD01,112659,,2018-01-23 00:00,,,2345,ZB501,Acme Widget hexangle,,,,3.00,99.00
VINT01,112681,,2018-01-23 00:00,,,2345,ZB501,Acme Widget hexangle,,,,1.00,33.00
RS01,112657,,2018-01-23 00:00,,,2345,ZB701,Acme Widget Circular 20 inch,,,,1.00,33.00
PIP01,112670,,2018-01-23 00:00,,,2345,ZB701,Acme Widget Circular 20 inch,,,,1.00,33.00
BPS01,112679,,2018-01-23 00:00,,,2345,ZB701,Acme Widget Circular 20 inch,,,,1.00,33.00
RE01,112663,,2018-01-23 00:00,,,2345,ZB901,Acme Widget Square 3 inch,,,,1.00,32.00
TB01,112665,,2018-01-23 00:00,,,2345,ZB901,Acme Widget Square 3 inch,,,,1.00,32.00
####
C:\SCRIPTS-PERL>perl inputfilescriptmods.pl
Enter the name of the file to read: input.csv
Enter the name of the file to write: output.csv
Can't locate object method "getline" via package "input.csv" (perhaps you forgot to load "input.csv"?) at inputfilescriptmods.pl line 36, line 2.
C:\SCRIPTS-PERL>
####
# grab user input..
print "Enter the name of the file to read: ";
my $filetoread = ;
chomp($filetoread);
print "Enter the name of the file to write: ";
my $filetowrite = ;
chomp($filetowrite);
# ****print "Enter the name of the output file: ";
# ****my $fileoutput = ;
# ****chomp($fileoutput);
# ****open $in, "new( { binary=>1 } );
open my $writehandle, '>', $filetowrite or die "Unable to write [$filetoread]!";
# ****open $out, ">output.csv" or die "output.csv: $!\n";
$csvo = Text::CSV->new( { binary=>1, eol=>$/ } );
$ar = $csvi->getline($filetoread); $col = 0;
# ****$ar = $csvi->getline($in); $col = 0;
foreach (@{$ar}) { $_ = "Col$col" if $col>7; s/ //; $col++; }
$csvi->column_names($ar); $csvo->column_names(@hdr); $csvo->print($out,\@hdr);
while ($hri = $csvi->getline_hr($in)) {
if ($hri->{InvDate}) {
($dd,$mm,$yyyy) = split("/",$hri->{InvDate});
$hro = { DeviceID=>$hri->{CustCode}, PosTxnID=>$hri->{InvNum}, MobileNumber=>'',
Timestamp=>$yyyy.'-'.$mm.'-'.$dd.' 00:00', PosID=>'', PosUserID=>'',
ShopID=>'2345', Prodcode=>$code, ProdDescription=>$desc, ProdDept=>'',
ProdGroup1=>'', ProdGroup2=>'', Qty=>$hri->{Qty}, Value=>$hri->{NetAmt} };
if ($spacer) { $csvo->print($out,\@e14); $spacer = 0; }
$csvo->print_hr($out,$hro);
}
elsif ($hri->{CustCode}) { ($code,$desc) = split(' - # ',$hri->{CustCode}); }
elsif ($hri->{LineDisc}) { $spacer = 0; }
}
# Original code had { $spacer = 1; } I changed this as my input.csv file originally had a - but then started arriving with - # between the Product code and product description in the input.csv file