From time to time I have to parse a csv file that also has names in it, usually lastname, firstname inside of quotes.
This is obnoxious if you do not want them separated and a PITA. The code below allows you to open a file and replace them with a different delineating character programmatically, so with the new format you can then key off of that new character to import into excel as a character delimited file. For the environment I'm in, I usually use the pipe character "|" .
cls
$originalFileLocation = "c:\temp\noncompatibleo365users.txt";
$newFileLocation = "c:\temp\noncompatibleo365usersParsed2.txt";
$fileLines = Get-Content($originalFileLocation);
$replacementCharacter = "|"; # set this to the character you want to use to delineate, instead of a comma
$counter = 0;
foreach ($fileLine in $fileLines)
{
$fileLineSplit = $fileLine.Split(",");
$newLine = "";
foreach ($partOfLine in $fileLineSplit)
{if (($partOfLine -notmatch '"') -and ($quoteFoundBlocker -eq $false))
{
$newPartOfLine = $partOfLine.Replace(",", $replacementCharacter);
$newLine += $newPartOfLine.Trim() + $replacementCharacter;
}
else
{
if (($partOfLine -match '"') -and ($quoteFoundBlocker -eq $false))
{
$quoteFoundBlocker = $true;
}
elseif (($partOfLine -match '"') -and ($quoteFoundBlocker -eq $true))
{
$quoteFoundBlocker = $false;
}
$newPartOfLine = $partOfLine.Trim();
if ($newPartOfLine.StartsWith('"'))
{
if ($newPartOfLine.EndsWith('"'))
{
$newLine += $partOfLine.Trim();
}
else
{
$newLine += $partOfLine.Trim() + ", ";
}
}
elseif ($newPartOfLine.Trim().EndsWith('"'))
{
$newLine += $partOfLine.Trim();
}
else
{
$newLine += $partOfLine.Trim() + ", ";
}
}
$newLine >> $newFileLocation;
$counter++;
Write-Host "Processed line count:" $counter;
}
and a function format
$returnValue = replaceDelineatorOutsideOfQuotes($sharedString);
function replaceDelineatorOutsideOfQuotes($fileLinesToDelineate)
{
$replacementCharacter = "|";
[bool] $quoteFoundBlocker = $false;
$newFileLines = New-Object System.Collections.ArrayList;
foreach ($fileLine in $fileLinesToDelineate)
{
$fileLineSplit = $fileLine.Split(",");
$newLine = "";
foreach ($partOfLine in $fileLineSplit)
{
if (($partOfLine -notmatch '"') -and ($quoteFoundBlocker -eq $false))
{
$newPartOfLine = $partOfLine.Replace(",", $replacementCharacter);
$newLine += $newPartOfLine.Trim() + $replacementCharacter;
}
else
{
if (($partOfLine -match '"') -and ($quoteFoundBlocker -eq $false))
{
$quoteFoundBlocker = $true;
}
elseif (($partOfLine -match '"') -and ($quoteFoundBlocker -eq $true))
{
$quoteFoundBlocker = $false;
}
$newPartOfLine = $partOfLine.Trim();
if ($newPartOfLine.StartsWith('"'))
{
if ($newPartOfLine.EndsWith('"'))
{
$newLine += $partOfLine.Trim();
}
else
{
$newLine += $partOfLine.Trim() + ", ";
}
}
elseif ($newPartOfLine.Trim().EndsWith('"'))
{
$newLine += $partOfLine.Trim();
}
else
{
$newLine += $partOfLine.Trim() + ", ";
}
}
}
$newFileLines.Add($newLine) | Out-Null;
} return $newFileLines; }
No comments:
Post a Comment