我有两个CSV文件作为例如:Powershell的比较的CSV
1)Records.csv
Account,TransactionDate,Description,Amount
Acct1,1/1/2015,Shopping at Kohls,($35.00)
Acct1,1/1/2016,Shopping at Target,($25.22)
Acct1,1/1/2016,Shopping at Target,($60.00)
Acct2,4/5/2015,Subway,($9.12)
Acct2,1/1/2016,Jimmy Johns,($11.00)
Acct3,1/1/2016,Shopping at Kohls,($35.00)
Acct1,1/1/2016,Shopping at Kohls,$35.00
Acct1,1/1/2016,Shopping at Target,($60.00)
Acct2,4/5/2015,Subway,($9.12)
Acct2,1/1/2016,Jimmy Johns,($11.00)
Acct2,1/1/2016,Jimmy Johns,($11.00)
Acct3,1/1/2016,Shopping at Kohls,($35.00)
Acct3,1/1/2016,Kroger,($27.00)
和2)rules.csv:
Description,Category,Topic
Shopping at Kohls,Misc,Clothing
Subway,Food,Restaurants
Jimmy Johns,Food,Restaurants
Shopping at Target,Misc,Home
Kroger,Food,Groceries
我需要附加类别并根据匹配的描述将rules.csv中的主题转换为records.csv文件。
当我运行以下代码时,它可以工作,但将rules.csv中的整行附加到一个字段中。我需要将它追加到单独的列中。
$rules = import-csv "rules.CSV"
import-csv "Records.csv" | % {
$desc=$_.Description
$_ | Add-Member NoteProperty "Category" ($rules | select-string -SimpleMatch $desc) -PassThru
} | export-csv "output.csv" -notype
输出看起来像这样,但我需要拆分最后一列。
"Account","TransactionDate","Description","Amount","Category"
"Acct1","1/1/2015","Shopping at Kohls","($35.00)","@{Description=Shopping at Kohls; Category=Misc; Topic=Clothing}"
"Acct1","1/1/2016","Shopping at Target","($25.22)","@{Description=Shopping at Target; Category=Misc; Topic=Home}"
"Acct1","1/1/2016","Shopping at Target","($60.00)","@{Description=Shopping at Target; Category=Misc; Topic=Home}"
"Acct2","4/5/2015","Subway","($9.12)","@{Description=Subway; Category=Food; Topic=Restaurants}"
"Acct2","1/1/2016","Jimmy Johns","($11.00)","@{Description=Jimmy Johns; Category=Food; Topic=Restaurants}"
"Acct3","1/1/2016","Shopping at Kohls","($35.00)","@{Description=Shopping at Kohls; Category=Misc; Topic=Clothing}"
"Acct1","1/1/2016","Shopping at Kohls","$35.00 ","@{Description=Shopping at Kohls; Category=Misc; Topic=Clothing}"
"Acct1","1/1/2016","Shopping at Target","($60.00)","@{Description=Shopping at Target; Category=Misc; Topic=Home}"
"Acct2","4/5/2015","Subway","($9.12)","@{Description=Subway; Category=Food; Topic=Restaurants}"
"Acct2","1/1/2016","Jimmy Johns","($11.00)","@{Description=Jimmy Johns; Category=Food; Topic=Restaurants}"
"Acct2","1/1/2016","Jimmy Johns","($11.00)","@{Description=Jimmy Johns; Category=Food; Topic=Restaurants}"
"Acct3","1/1/2016","Shopping at Kohls","($35.00)","@{Description=Shopping at Kohls; Category=Misc; Topic=Clothing}"
"Acct3","1/1/2016","Kroger","($27.00)","@{Description=Kroger; Category=Food; Topic=Groceries}"
有没有办法做到这一点?
您需要_“分裂的最后一列,“_ 怎么样?你希望'Category'和'Topic'也被抽出? “描述”看起来已经完好无损并且呈现,所以您还可以提供一些您期望*输出的内容,以节省每个人的时间和任何困惑? :) – gravity
@gravity谢谢。是的,我需要的类别和主题匹配的描述拉出。列标题可以是类别和主题。我不想在上面显示的这个数组表示法中使用它。再次感谢。 –