Dear Monks,
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.
this is my 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: Column name
Based on header elements like "ESS" means that corresponding column's row "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" other wise will be an empty cell
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
, , , , , , , , , "OC192-11,OC192:ESS,0,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 CS +SP PSC VLDTY TMPER CMP CMPMX LSDS LUAS INMACC +ONTROLFR FRTOO UTL UTLMX INFRAMES OUTFRAMES INFRAME +SERR 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 o +therwise we can use ARGV[0] my $newFile = "csvfile.csv"; open(my $outFileHandle, '>', $newFile) or die "cannot open $newFile fo +r writing: $!"; open(my $IN, '<', $InputFile) or die "cannot open $InputFile for writi +ng: $!"; 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 fi +nd 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_g +roup\""; foreach my $head(@found_header) { if($head =~ m/^(.*),((.*)\ +:([A-Z_]+),(.*))$/g) { $subhead = $1; $mainhead = $4 +; $roothead = $1 +.",".$2; if($subhead =~ + m/-(\d+)/g) { $n +umval = $1; fo +reach $header_items(@header) { + if($mainhead eq $header_items && $numval == $unique_group) + { + print $outFileHandle $roothead; + } } } } } } close $IN;
Thanks and Regards,
Senthil. V
In reply to Row wise print the values when convert the cvs file . by senthil_v
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |