Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell比较CSV_Csv_Powershell - Fatal编程技术网

Powershell比较CSV

Powershell比较CSV,csv,powershell,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 Jo

我有两个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}"
有什么办法吗?

用这个

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

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}}
        }
    }
}

你需要“拆分最后一列”,怎么做?你想把
类别
主题
也拉出来吗<代码>说明看起来已经完好无损,因此您能否提供几行您希望输出的内容,以节省大家的时间和任何混乱?:)@谢谢。是的,我需要的类别和主题匹配的描述拉出来。列标题可以是类别和主题。我不希望它出现在我上面展示的这个数组符号中。再次感谢。谢谢-这似乎正是我需要的。然而,我是这样一个新手,我甚至不知道如何导入一个PS文件,使这些功能可用。在谷歌上搜索,一个小时后尝试——现在在Mac电脑上,然后做其他事情。谢谢。找到了包含.ps1文件的方法。基本上\MyFunctions.ps1将很快重试。谢谢。再次感谢你帮助我。倾向于较小的脚本。我试过了,效果很好,只是它省去了没有匹配“规则”的“记录”。我试着写一个“else”语句,但没有得到预期的结果。基本上,如果规则csv中没有匹配的描述,我们应该得到一个空的类别和主题。希望这是有道理的。谢谢`$a=Import Csv“records.Csv”$b=Import Csv“rules.Csv”$rules_app=foreach($a中的记录){$unmatched=$true foreach($b中的obj){if($obj.Description-eq$record.Description){$record | select*,@{n='Category';e={$obj.Category},@{n='Topic e';e={$obj.Topic unmatched}$obj.Topic{$record | select*,@{n='Category';e=“请选择”},@{n='Topic';e=“请输入”}}}}$rules_app | Export csv“rules_applicated.csv”-notype信息`
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}}
        }
    }
}