Powershell比较CSV

时间:2016-07-29 20:13:44

标签: 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}"

有什么办法吗?

1 个答案:

答案 0 :(得分:2)

使用了这个

https://github.com/RamblingCookieMonster/PowerShell/blob/master/Join-Object.ps1

$a = @'
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)
'@ | ConvertFrom-Csv

$b = @'
Description,Category,Topic
Shopping at Kohls,Misc,Clothing
Subway,Food,Restaurants
Jimmy Johns,Food,Restaurants
Shopping at Target,Misc,Home
Kroger,Food,Groceries
'@ | ConvertFrom-Csv

Join-Object $a $b -LeftJoinProperty description -RightJoinProperty description

# output:
# Account TransactionDate Description        Amount   Category Topic      
# ------- --------------- -----------        ------   -------- -----      
# Acct3   1/1/2016        Kroger             ($27.00) Food     Groceries  
# Acct2   4/5/2015        Subway             ($9.12)  Food     Restaurants
# Acct2   4/5/2015        Subway             ($9.12)  Food     Restaurants
# Acct1   1/1/2016        Shopping at Target ($25.22) Misc     Home       
# Acct1   1/1/2016        Shopping at Target ($60.00) Misc     Home       
# Acct1   1/1/2016        Shopping at Target ($60.00) Misc     Home       
# Acct2   1/1/2016        Jimmy Johns        ($11.00) Food     Restaurants
# Acct2   1/1/2016        Jimmy Johns        ($11.00) Food     Restaurants
# Acct2   1/1/2016        Jimmy Johns        ($11.00) Food     Restaurants
# Acct1   1/1/2015        Shopping at Kohls  ($35.00) Misc     Clothing   
# Acct3   1/1/2016        Shopping at Kohls  ($35.00) Misc     Clothing   
# Acct1   1/1/2016        Shopping at Kohls  $35.00   Misc     Clothing   
# Acct3   1/1/2016        Shopping at Kohls  ($35.00) Misc     Clothing   

*编辑:如果你不想使用其他脚本,这也应该有效

foreach ($record in $a) {
    foreach ($obj in $b) {
        if ($obj.description -eq $record.description) {
            $record | select *, @{n='Category';e={$obj.Category}}, @{n='Topic';e={$obj.Topic}}
        }
    }
}