1

我正在尝试比较两个 csv 文件,第一个称为 ticket.csv 并包含列标题:“tcn”、“two”、“twod”。第二个文件名为 tracker.csv,包含标题:“cn”、“wo”和“wod”。我想输出tracker.csv的内容,如果有“tcn”(tickets.csv)匹配“cn”(tracker.csv),则将其关联的“二”(tickets.csv)写入“wo”( tracker.csv) 和 "twod" (tickets.csv) 到 "wod" (tracker.csv)。

我遇到了我的代码没有移动“两个”或“wod”的问题,并且它还编写了每个对象 4 次。

输入 CSV

Import-Csv "$home\Desktop\nestTest\tracker.csv" |
ForEach-Object {
    $machine = $_.machine
    $wo = ""
    $wod = ""
    $tickets = import-csv "$home\Desktop\nestTest\tickets.csv"
    foreach ($ticket in $tickets) {
        try {
        $tcn = ""
        $two = ""
        $twod = ""
        $two = $_.wo
        $twod = $_.wod
        $tcn = $_.cn
        if ($tcn -eq $machine) {
            $wo = $two
            $wod = $twod
        }
        else {
        $wo = ""
        $wod = ""
        }
        }
        catch {
        }
        [pscustomobject]@{
        "Machine Name"=$machine
        "Work Order #"=$wo
        "Work Order Date"=$wod
        }
     }
} |
        select "Machine Name","Work Order #","Work Order Date" |
        Export-Csv "$home\Desktop\nestTest\output.csv"
4

1 回答 1

0

我之前看错了方向。但我想通了。

$tracker = Import-Csv "$home\Desktop\nestTest\tracker.csv" |
ForEach-Object {
    $machine = ""
    $wo = ""
    $wod = ""
    $machine = $_.machine
    $tickets = import-csv "$home\Desktop\nestTest\tickets.csv" |
    ForEach-Object {
        $two = ""
        $twod = ""
        $two = $_."wo"
        $twod = $_."wod"
        $tcn = $_."cn"
        if ($tcn -eq $machine) {
            $wo = $two
            $wod = $twod
        }
        else {
        }
    }
        [pscustomobject]@{
        "Machine Name"=$machine
        "Work Order #"=$wo
        "Work Order Date"=$wod
        }

} |
select "Machine Name","Work Order #","Work Order Date" |
Export-Csv "$home\Desktop\nestTest\output.csv" -NoTypeInformation
于 2020-04-16T22:43:32.623 回答