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;