Hi friends. Below is the sample of my csv file
Amount ChoiceId Allowed PID AmountId MultiplyingFactor + PF new ServiceTax ExtraTalkTime MRP Recharge Type FRC +/SRC 13 na 285 100 1.1236 81.75 0.02 0 92 Freebi +e(BA)+PID Normal 59 na 285 101 1.1236 8.31 0.07 0 10 Freebie +(BA)+PID Normal 13 na 289 100 1.1236 81.75 0.05 0 92 Freebi +e(BA)+PID Normal 2001 na 285 103 1.103 27.13 2.06 0 52 MA+Da +taPack+PID Normal 1572 na 285 104 na 0 na na na Blank MRP, re +st OK Normal 1574 na 285 105 na 0 na na na Blank MRP, re +st OK Normal 333300 na 285 106 1 0 0 0 3333 Freebie(BA)+ +MA+PID Normal 93 na 285 107 1.1236 100.53 0.11 0 114 Free +bie(BA)+PID+SA Normal 78 na 285 108 1.1236 8.12 0.1 0 10 Freebie( +BA)+PID Normal 79 na 285 109 1.1236 85.54 0.1 0 97 BA20 on +ly Normal 13 na 290 100 1.1236 81.75 0.11 0 92 Freebi +e(BA)+PID Normal
now as per the requirement, if values of amount, amountId and MRP are same for particular rows then in that case i need to replace it by only one row in which value of AllowdPID and Service tax will take values from all the input the rows, separated by semi colon..(rest of the values we will not manipulate as they will be equal automatically)... for example as the amount, amountId and MRP values for 1st, 3rd and last rows are equal. our output will contain only one line in place of these three line like below.
13 na 285;289;290 100 1.1236 81.75 0.02;0.05;0.11 + 0 92 Freebie(BA)+PID Normal
how can I achieve it using perl. I tried someting using Text::CSV but can't share as it becomes a total mess :(...please help.
below is my script
#! /usr/bin/perl use strict; use warnings; use Text::CSV; my $basepath = "/home/eankuls/perl/vodafone/punjab"; my $IPfile = "Consolidated_output.csv"; my $OPfile = "Final_report.csv"; open (my $FH, "${basepath}/${IPfile}") or die "Can't open $IPfile file +: $!"; open (my $WFH, ">>", "${basepath}/${OPfile}") or die "Can't open $OPfi +le file: $!"; my $csv = Text::CSV->new({ sep_char => ",", binary => 1, auto_diag => 1 }); my $row = $csv->getline($FH); my $flag = 0; my $flag1 = 0; my (@row1, @row2); while (my $row = $csv->getline($FH) ) { my (@AllowedPID, @STax); if ($flag == 0) { @row1 = @{$row}; $flag++; push (@AllowedPID, $row1[2]); push (@STax, $row1[6]); next; } else { @row2 = @{$row}; } if ($row1[0] == $row2[0] and $row1[3] == $row2[3] and $row1[8] +== $row2[8]) { if ($flag1 == 0 ) { push (@AllowedPID, $row1[2]); push (@STax, $row1[6]); $flag1++; } push (@AllowedPID, $row2[2]); push (@STax, $row2[6]); next; } else { $row2[2] = join(";", @AllowedPID); $row2[6] = join(";", @STax); my $row_final = join (",", @row2); print $WFH $row_final; @row1 = @row2; next; } #print WFH @row2; $flag = 0; $flag1 = 0; }
In reply to perl: How to comapre and manipulate csv file in perl? by Ankur_kuls
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |