问题
I need to separate the following array data but can't think of a good and quick way to do it.
Name OrderGroup OrderDate
PID365583 FY13Q1-D 8/28/2014 12:00:00AM
PID354731,361935 FY13Q2-D 8/28/2014 12:00:00 AM
PID354737,361937 FY13Q3-D 11/7/2014 12:00:00 AM
PID359099,361933,363165 FY13Q4-D 11/13/2014 12:00:00 AM
Every name that has more than one number associated with it (separated by commas) I need to move to a separate line in the array and copy the same info from the array line that it's currently in. So PID354731,361935
would need to be broken into two lines, one for PID354731
and one for 361935
; both would contain the same ordergroup FY13Q2-D
and order date 8/28/2014 12:00:00 AM
.
回答1:
Assuming file data:
foreach ($line in (Get-Content $file | select -skip 1) )
{
$Parts = $line.split(' ',3)
foreach ($Name in $Parts[0].split(',') )
{
[PSCustomObject]@{
Name = $Name
OrderGroup = $Parts[1]
OrderDate = [datetime]$Parts[2]
}
}
}
来源:https://stackoverflow.com/questions/28057283/splitting-string-field-and-repeating-other-fields-in-output-objects