1

I'm receiving an automated report from a system that cannot be modified as a CSV. I am using PowerShell to split the CSV into multiple files and parse out the specific data needed. The CSV contains columns that may contain no data, 1 value, or multiple values that are comma separated within the CSV file itself.

Example(UPDATED FOR CLARITY):

"Group","Members"  
"Event","362403"  
"Risk","324542, 340668, 292196"  
"Approval","AA-334454, 344366, 323570, 322827, 360225, 358850, 345935"  
"ITS","345935, 358850"  
"Services",""  

I want the data to have one entry per line like this (UPDATED FOR CLARITY):

"Group","Members"  
"Event","362403"  
"Risk","324542"  
"Risk","340668"  
"Risk","292196"  
#etc.

I've tried splitting the data and I just get an unknown number of columns at the end.

I tried a foreach loop, but can't seem to get it right (pseudocode below):

Import-CSV $Groups
ForEach ($line in $Groups){
    If($_.'Members'.count -gt 1, add-content "$_.Group,$_.Members[2]",)}

I appreciate any help you can provide. I've searched all the stackexchange posts and used Google but haven't been able to find something that addresses this exact issue.

1
  • I have updated the source and output for better clarity. Commented Sep 3, 2019 at 17:17

3 Answers 3

1
Import-Csv .\input.csv | ForEach-Object {
    ForEach ($Member in ($_.Members -Split ',')) {
        [PSCustomObject]@{Group = $_.Group; Member = $Member.Trim()}
    }
} | Export-Csv .\output.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

Comments

0
# Get the raw text contents
$CsvContents = Get-Content "\path\to\file.csv"

# Convert it to a table object
$CsvData = ConvertFrom-CSV -InputObject $CsvContents

# Iterate through the records in the table
ForEach ($Record in $CsvData) {
    # Create array from the members values at commas & trim whitespace
    $Record.Members -Split "," | % {
        $MemberCount = $_.Trim()
        # Check if the count is greater than 1
        if($MemberCount -gt 1) {
            # Create our output string
            $OutputString = "$($Record.Group), $MemberCount"
            # Write our output string to a file
            Add-Content -Path "\path\to\output.txt" -Value $OutputString
        }
    }
}

This should work, you had the right idea but I think you may have been encountering some syntax issues. Let me know if you have questions :)

1 Comment

Thank you, this got me 99% of the way there. For some reason if the member number starts with a zero then it doesn't include the value in the output. For example I had a "CBF","09091,039600" and it did not get output at all, whereas a "LAB","3394177,081593, 112796, 200741" included all the entries except the one that started with zero.
0

Revised the code as per your updated question,

$List = Import-Csv "\path\to\input.csv"

foreach ($row in $List) {
    $Group = $row.Group
    $Members = $row.Members -split ","

    # Process for each value in Members
    foreach ($MemberValue in $Members) {
        # PS v3 and above
        $Group + "," + $MemberValue | Export-Csv "\path\to\output.csv" -NoTypeInformation -Append
        # PS v2
        # $Group + "," + $MemberValue | Out-File "\path\to\output.csv" -Append
    }
}

1 Comment

Thank you, this got me 99% of the way there but has the same problem as the other solution. For some reason if the member number starts with a zero then it doesn't include the value in the output. For example I had a "CBF","09091,039600" and it did not get output at all, whereas a "LAB","3394177,081593, 112796, 200741" included all the entries except the one that started with zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.