Splitting string field and repeating other fields in output objects

╄→гoц情女王★ 提交于 2020-02-08 06:57:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!