I am trying to make a CSV file out of an LDAP output that comes from an external process.
Here is the structure of the output file:
cn: applegroup#!/usr/bin/perl use strict; use warnings; # 0a) Initialize variables my $grpNm = ""; my $memID = ""; my @groupInfo = ("", ""); my @memberInfo = ("", ""); my @ldapMemInfo = ("", ""); my @idInfo = ("", ""); my @csvContent = ("", ""); my $allGroupResult = "allTmp.txt"; my $allGroupDataFinal = "allData.csv"; open my $fhGroupFile, '<', "$allGroupResult" or die "Could not create +or open $allGroupResult"; open my $fhCSVOutput, '>', "$allGroupDataFinal" or die "Could not crea +te or open $allGroupDataFinal"; sub lTrim { my $s=shift; $s =~ s/^\s+//; return $s; } sub rTrim { my $s=shift; $s =~ s/\s+$//; return $s; } sub trimAll { my $s=shift; $s =~ s/^\s+|\s+$//g; return $s; } # ------------ Main Program -------------------- while (<$fhGroupFile>) { my $line = $_; chomp $line; # Check if line begins with "cn" or "member" if ($line =~ m/^cn/) # found group name { @groupInfo = split /:/, $line; $grpNm = $groupInfo[1]; # second entry is group name $grpNm = lTrim($grpNm); #trim leading spaces from group name # print "array indice $groupInfo[1]\n"; print "scalar grpNm:$grpNm\n"; $csvContent[0] = $grpNm; } elsif ($line =~ m/^member/) # found member name { @memberInfo = split /:/, $line; @ldapMemInfo = split /,/, $memberInfo[1]; # second entry is full + string for ldap content @idInfo = split /=/, $ldapMemInfo[0]; # second entry is ldap con +tent #print "array idInfo /= @idInfo\n\n"; $memID = $idInfo[1]; # second entry is the specific ID #print "scalar memID = $memID\n"; $csvContent[1] = $memID; print $fhCSVOutput "$csvContent[0]"; print $fhCSVOutput ","; #print $fhCSVOutput "$csvContent[1]"; print $fhCSVOutput "\n"; } else { } } # while <$fhGroupFile> close($fhGroupFile); close($fhCSVOutput);
When I run it I get:
,pplegroup
,pplegroup
,pplegroup
,pplegroup
,rangegroup
,rangegroup
Before you ask, I get the same result if I use the scalar variables without the array @csvContent so,
print $fhCSVOutput "$csvContent[0]"; print $fhCSVOutput ","; #print $fhCSVOutput "$csvContent[1]"; print $fhCSVOutput "\n";
AND
print $fhCSVOutput "$grpNm"; print $fhCSVOutput ","; #print $fhCSVOutput "$memID"; print $fhCSVOutput "\n";
In reply to Strings overwrite themselves in print command by tdsny71
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |