使用多个和重复的分隔符在CSV文件中拆分列值

时间:2016-01-21 08:28:23

标签: csv powershell powershell-v3.0

我有一个混乱的CSV文件。我正在尝试使用正则表达式从csv文件的列中的值中提取名字和姓氏。名字和姓氏将有自己的列。

CSV文件(使用不同的分隔符组合):

ID,Description,Number
JDo,John Doe - Temp - Client Client Ops,SomeValue
JDo,John  Doe - Temp - Client Client Ops,SomeValue
JDo,John  Doe  - Temp - Client Client Ops,SomeValue
JDo,John  Doe  -  Temp - Client Client Ops,SomeValue
JDo,John  Doe  -  Temp  - Client Client Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client Client Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client  Client Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client  Client  Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client  Client  Ops ,SomeValue
JDo,John  Doe  -  Temp  -  Client  Client  Ops  ,SomeValue
JDo,John Doe-Temp-Client Client Ops,SomeValue
JDo,John  Doe - Temp-Client Client Ops,SomeValue
JDo,John  Doe  - Temp-Client Client Ops,SomeValue
JDo,John  Doe-Temp -  Client Client Ops,SomeValue
JDo,John  Doe  -  Temp  - Client Client Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client Client Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client  Client Ops,SomeValue
JDo,John  Doe  -  Temp  -  Client  Client  Ops,SomeValue
JDo,John  Doe-Temp  -  Client  Client  Ops ,SomeValue
JDo,John  Doe-Temp-Client  Client  Ops  ,SomeValue
JDo,John.Doe - Temp - Client Client Ops,SomeValue
JDo,John .Doe - Temp - Client Client Ops,SomeValue
JDo,John. Doe - Temp - Client Client Ops,SomeValue
JDo,John . Doe - Temp - Client Client Ops,SomeValue
JDo,John.Doe - Temp - Client Client Ops  ,SomeValue
JDo,John .Doe - Temp - Client Client Ops  ,SomeValue
JDo,John. Doe - Temp - Client Client Ops  ,SomeValue
JDo,John . Doe - Temp - Client Client Ops  ,SomeValue
JDo,John.Doe-Temp-Client Client Ops,SomeValue
JDo,John .Doe-Temp-Client Client Ops,SomeValue
JDo,John. Doe-Temp-Client Client Ops,SomeValue
JDo,John . Doe-Temp-Client Client Ops,SomeValue
JDo,John.Doe  - Temp  - Client Client Ops,SomeValue
JDo,John .Doe -  Temp -  Client Client Ops,SomeValue
JDo,John. Doe  -  Temp  -  Client Client Ops,SomeValue
JDo,John . Doe - Temp - Client Client Ops,SomeValue
JDo,John?Doe - Temp - Client Client Ops,SomeValue
JDo,John ?Doe - Temp - Client Client Ops,SomeValue
JDo,John? Doe - Temp - Client Client Ops,SomeValue
JDo,John ? Doe - Temp - Client Client Ops,SomeValue
JDo,John?Doe - Temp - Client Client Ops  ,SomeValue
JDo,John ?Doe - Temp - Client Client Ops  ,SomeValue
JDo,John? Doe - Temp - Client Client Ops  ,SomeValue
JDo,John ? Doe - Temp - Client Client Ops  ,SomeValue
JDo,John?Doe-Temp-Client Client Ops,SomeValue
JDo,John ?Doe-Temp-Client Client Ops,SomeValue
JDo,John? Doe-Temp-Client Client Ops,SomeValue
JDo,John ? Doe-Temp-Client Client Ops,SomeValue
JDo,John?Doe  - Temp  - Client Client Ops,SomeValue
JDo,John ?Doe -  Temp -  Client Client Ops,SomeValue
JDo,John? Doe  -  Temp  -  Client Client Ops,SomeValue
JDo,John ? Doe - Temp - Client Client Ops,SomeValue
JDo,"John,Doe - Temp - Client Client Ops",SomeValue
JDo,"John ,Doe - Temp - Client Client Ops",SomeValue
JDo,"John, Doe - Temp - Client Client Ops",SomeValue
JDo,"John , Doe - Temp - Client Client Ops",SomeValue
JDo,"  John,Doe - Temp - Client Client Ops  ",SomeValue
JDo,"  John ,Doe - Temp - Client Client Ops  ",SomeValue
JDo,"  John, Doe - Temp - Client Client Ops  ",SomeValue
JDo,"  John , Doe - Temp - Client Client Ops  ",SomeValue
JDo,"John,Doe-Temp-Client Client Ops",SomeValue
JDo,"John ,Doe-Temp-Client Client Ops",SomeValue
JDo,"John, Doe-Temp-Client Client Ops",SomeValue
JDo,"John , Doe-Temp-Client Client Ops",SomeValue
JDo,"John,Doe  - Temp  - Client Client Ops",SomeValue
JDo,"John ,Doe -  Temp -  Client Client Ops",SomeValue
JDo,"John, Doe  -  Temp  -  Client Client Ops",SomeValue
JDo,"John , Doe - Temp - Client Client Ops",SomeValue
JDo,John-Doe - Temp - Client Client Ops,SomeValue
JDo,John -Doe - Temp - Client Client Ops,SomeValue
JDo,John- Doe - Temp - Client Client Ops,SomeValue
JDo,John - Doe - Temp - Client Client Ops,SomeValue
JDo,John-Doe - Temp - Client Client Ops  ,SomeValue
JDo,John -Doe - Temp - Client Client Ops  ,SomeValue
JDo,John- Doe - Temp - Client Client Ops  ,SomeValue
JDo,John - Doe - Temp - Client Client Ops  ,SomeValue
JDo,John-Doe-Temp-Client Client Ops,SomeValue
JDo,John -Doe-Temp-Client Client Ops,SomeValue
JDo,John- Doe-Temp-Client Client Ops,SomeValue
JDo,John - Doe-Temp-Client Client Ops,SomeValue
JDo,John-Doe  - Temp  - Client Client Ops,SomeValue
JDo,John -Doe -  Temp -  Client Client Ops,SomeValue
JDo,John- Doe  -  Temp  -  Client Client Ops,SomeValue
JDo,John - Doe - Temp - Client Client Ops,SomeValue

要添加名字和姓氏列,我使用以下代码:

<datalist id="mylist">
   <option value="a">
   <option value="b">
   <option value="b">
</datalist>

<select class="someSelect">
<select class="someSelect">

$(".someSelect").html( $("#mylist").html() );

当我运行此代码时,所有名字值应为John,所有姓氏值应为Doe。但是,所有人的价值观都非常不同。

1 个答案:

答案 0 :(得分:3)

你认为太复杂了。从Description字段的末尾删除附加信息以获取名称,然后修剪名称并将其拆分为名字和姓氏,然后将这些名称作为新属性添加到输入对象。

试试这个:

Import-Csv 'C:\path\to\input.csv' | ForEach-Object {
  $rawname = $_.Description -replace '-[^-]*-[^-]*$'
  $firstname, $lastname = $rawname.Trim() -split ' *[ \?\.,-] *'
  $_ | Add-Member -Type NoteProperty -Name FirstName -Value $firstname
  $_ | Add-Member -Type NoteProperty -Name LastName -Value $lastname
  $_
} | Export-Csv 'C:\path\to\output.csv' -NoType