Dear Experts,
Here i am written code for convert the text to CSV file conversion. i am facing a problem in while compare the two array to print the values row wise. below i have mentioned my code,input and output format.
input :
"OC192-11,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:OPRN,58,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:OPRN,59,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
output:
EQP_NAME, TID, AID, LOCN, DIRN, EQP_TYPE, AID_TYPE, TIMESTAMP, ESS, SESS, CVS, SEFSS, ESL, SESL, CVL, UASL, FCL, PSD, PSCW, PSCP, ESP, SESP, SASP, CVP, ALSP, UASP, FCP, ES, SES, UAS, LDS, SEFSP, CSSP, PSC, VLDTY, TMPER, CMP, CMPMX, LSDS, LUAS, INMACCONTROLFR, FRTOO, UTL, UTLMX, INFRAMES, OUTFRAMES, INFRAMESERR, INFRAMESDISC, OUTFRAMESDISC, FRTOOSHORTS, FRTOOLONGS, REC_RELIABILITY_IND, PART_MAP, OPR, OPT, OPRN
,,,,,,,,"OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"OC192:OPRN,58,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
, , , , , , , ,"OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SEFSS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:ESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:SESL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:CVL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:UASL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1","OC192:FCL,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1", , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,"OC192:OPRN,59,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"
, , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , ,,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
use strict;
use warnings;
use Win32;
my @header = qw(EQP_NAME TID AID LOCN DIRN EQP_TYPE AID_TYPE TIMESTAMP ESS SESS CVS SEFSS ESL SESL CVL UASL FCL PSD PSCW PSCP ESP SESP SASP CVP ALSP UASP FCP ES SES UAS LDS SEFSP CSSP PSC VLDTY TMPER CMP CMPMX LSDS LUAS INMACCONTROLFR FRTOO UTL UTLMX INFRAMES OUTFRAMES INFRAMESERR INFRAMESDISC OUTFRAMESDISC FRTOOSHORTS FRTOOLONGS REC_RELIABILITY_IND PART_MAP OPR OPT OPRN);
print "Converting as CSV...\n";
sleep(1);
my @found_header = ();
my %seen = ();
my @unique = ();
my @number = ();
my $unique_group;
my $subhead;
my $mainhead;
my $roothead;
my $numval;
my $header_items;
my @modified_header = map { "$_," } @header;
my $InputFile = "sample_log_file.txt"; #directly coded the file name otherwise we can use ARGV[0]
my $newFile = "csvfile.csv";
open(my $outFileHandle, '>', $newFile) or die "cannot open $newFile for writing: $!";
open(my $IN, '<', $InputFile) or die "cannot open $InputFile for writing: $!";
print $outFileHandle "@modified_header,";
print $outFileHandle "\n";
while (my $line = <$IN>)
{
$line =~ s{(\"| )}{}gi; #Replace the Double quotes and space
if($line =~ m/^(.*),((.*)\:([A-Z_]+),(.*))\n$/g) ##Here To find Header values at group 4
{
#print $outFileHandle "\n";
foreach my $head(@header)
{
if($head eq $4)
{
push(@found_header, "\"$1,$2\",");
}
}
}
}
foreach my $found_header(@found_header)
{
# OC192-12,OC192:OPRN,59,COMPL,NEND,RCV,1-DAY,02-11,00-00,1",
if($found_header =~ m/([A-Z0-9-,]+):/g)
{
my $sub_value=$1;
if($sub_value =~ m/-(\d+),/g)
{
push(@number, $1);
}
}
}
foreach my $elem (@number)
{
next if $seen{ $elem }++;
push @unique, $elem;
}
foreach my $unique_group(@unique)
{
print $outFileHandle "\n";
#print $outFileHandle "\"$unique_group\"";
foreach my $head(@found_header)
{
if($head =~ m/^(.*),((.*)\:([A-Z_]+),(.*))$/g)
{
$subhead = $1;
$mainhead = $4;
$roothead = $1.",".$2;
if($subhead =~ m/-(\d+)/g)
{
$numval = $1;
foreach $header_items(@header)
{
if($mainhead eq $header_items && $numval == $unique_group)
{
print $outFileHandle $roothead;
}
}
}
}
}
}
close $IN;