Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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 使用StreamReader在每行的多个列上复制Vlookup的问题_Powershell_Csv_Streamreader - Fatal编程技术网

Powershell 使用StreamReader在每行的多个列上复制Vlookup的问题

Powershell 使用StreamReader在每行的多个列上复制Vlookup的问题,powershell,csv,streamreader,Powershell,Csv,Streamreader,我有一个非常大的管道分隔文件(~1000000行),其中包含11列,其中8列包含一个键 Master_File.csv 我还有一个小的管道分隔文件(~8k行),它包含两列,键和一个值 查找文件.csv 我需要遍历Master_File.csv的每一行,并用Lookup_File.csv中的值替换键。结果应该是这样的 Year|Key1|Key2|Key3|Key4|Key5|Key6|Key7|Key8|Location|Name 2019|Value 1235|Value 2345|Value

我有一个非常大的管道分隔文件(~1000000行),其中包含11列,其中8列包含一个键

Master_File.csv 我还有一个小的管道分隔文件(~8k行),它包含两列,键和一个值

查找文件.csv 我需要遍历Master_File.csv的每一行,并用Lookup_File.csv中的值替换键。结果应该是这样的

Year|Key1|Key2|Key3|Key4|Key5|Key6|Key7|Key8|Location|Name
2019|Value 1235|Value 2345|Value 1231|Value 1235|Value 3536|Value 1231|Value 1234|Value 3624|Site6|Storage
2019|Value 2345|Value 2345|Value 1231|Value 1231|Value 3536|Value 1235|Value 1234|Value 1231|Site8|Storage
2019|Value 3536|Value 2345|Value 3536|Value 1235|Value 2345|Value 1235|Value 3536|Value 1235|Site7|Storage
2019|Value 2345|Value 1235|Value 1231|Value 1235|Value 1231|Value 3452|Value 1231|Value 2345|Site9|Storage
下面的代码是到目前为止我所拥有的,但问题是它只在Master_文件中运行一次,只替换第一个键

$content = "c:\Lookup_File.csv"
$LookupFile = New-Object -TypeName System.IO.StreamReader - ArgumentList $content
$skip1 = $LookupFile.ReadLine()

$src = "c:\Master_File.csv"
$MasterFile = New-Object -TypeName System.IO.StreamReader -ArgumentList $src

$header = $Masterfile.ReadLine()
$outData = New-Object -TypeName System.Text.StringBuilder
[void]$outData.AppendLine($header)

while ($line = $LookupFile.ReadLine())
{
    $key = ($line -split "\|")[0]
    $value = ($line -split "\|")[1]
    while ($row = $MasterFile.ReadLine()){
        $row = $row -Replace $key, $value
        [void]$outData.AppendLine($row)
    }
}
$outData.ToString() | Out-File -FilePath "c:\Master_File_Out.csv" -Encoding ascii
我把这个作为输出

Year|Key1|Key2|Key3|Key4|Key5|Key6|Key7|Key8|Location|Name
2019|Value 1235|2345|1231|Value 1235|3536|1231|1234|3624|Site7|Storage
2019|2345|2345|1231|1231|3536|Value 1235|1234|1231|Site8|Storage
2019|3536|2345|3536|Value 1235|2345|Value 1235|3536|Value 1235|Site9|Storage
2019|2345|Value 1235|1231|Value 1235|1231|3452|1231|2345|Site11|Storage
我真的很难遍历Master_文件的每一行,并用值替换8个键中的每一个


任何帮助都将不胜感激

根据我的评论,尝试:

## Q:\Test\2019\08\26\SO_57659677.ps1

$hash = @{}
Import-Csv '.\Lookup_File.csv' -Delimiter '|' | ForEach {$Hash[$_.Key]=$_.Value}

$Master = Import-Csv '.\Master_File.csv' -Delimiter '|'

ForEach($Item in $Master){
    For($i=1;$i -le 8;$i++){
       if($hash.ContainsKey($item."Key$i")){
           $item."Key$i" = $hash[$item."Key$i"]
       }
    }
}

$Master |ft -auto

# $Master | Export-Csv "c:\Master_File_Out.csv" -Delimiter '|' -NoTypeInformation 
# To have no double quotes in the ouptput file
# ($Master | ConvertTo-Csv -Delimiter '|' -NoTypeInformation) -replace '"' | Set-Content -"c:\Master_File_Out.csv" Encoding ascii
样本输出:

> $Master|ft -auto

Year Key1       Key2       Key3       Key4       Key5       Key6       Key7       Key8       Location
---- ----       ----       ----       ----       ----       ----       ----       ----       --------
2019 Value 1235 Value 2345 Value 1231 Value 1235 Value 3536 Value 1231 Value 1234 Value 3624 Site6
2019 Value 2345 Value 2345 Value 1231 Value 1231 Value 3536 Value 1235 Value 1234 Value 1231 Site8
2019 Value 3536 Value 2345 Value 3536 Value 1235 Value 2345 Value 1235 Value 3536 Value 1235 Site7
2019 Value 2345 Value 1235 Value 1231 Value 1235 Value 1231 Value 3452 Value 1231 Value 2345 Site9
编辑 通过将列名读入数组并使用索引1..8添加了一个抽象级别

## Q:\Test\2019\08\26\SO_57659677_2.ps1
$hash = @{}
Import-Csv '.\Lookup_File.csv' -Delimiter '|' | ForEach {$Hash[$_.Key]=$_.Value}

$Master = Import-Csv '.\Master_File.csv' -Delimiter '|'
$ColNames = $Master[0].psobject.properties.name

ForEach($Item in $Master){
    For($i=1;$i -le 8;$i++){
       if($hash.ContainsKey($item."$($ColNames[$i])")){
           $item."$($ColNames[$i])" = $hash[$item."$($ColNames[$i])"]
       }
    }
}

$Master |ft -auto

几乎一样

$hash=@{}
导入csv查找_file.csv-分隔符“|”| foreach{
$hash[$\.key]=$\.value
}
导入csv master_file.csv-分隔符“|”| foreach{
foreach($1..8中的i){
$\.“key$i”=$hash[$\.“key$i”]
}
$_
}|导出csv主文件_out.csv-分隔符“|”

您可能会遇到密钥不唯一的问题(即,“1234”列出两次)。请将文件视为
.csv
文件,并在读取查找文件时使用
导入csv
/
导出csv
(带-分隔符“|”),构建哈希表。然后迭代Master_File.csv并替换哈希表中的字段值。很抱歉,在我的示例数据中,重复键是一个o类型。请告诉我。我想找出“关键$I”部分。我可能会使用foreach对象,这样我就可以通过管道将其导出到export-csv。正如我在解释中所写的那样。然而,我过度简化了我的示例数据。主文件中的键列不包含序号。它们是数据的快照。标题实际上就像今年一样,工作、批准、拒绝、WhatIf1、Whatif2、提交、提议、同步、位置。那么您希望如何处理这些列?第二、第九栏?当第一列年份可能会干扰数字时,将整行视为文本不是一个好主意。
> $Master|ft -auto

Year Key1       Key2       Key3       Key4       Key5       Key6       Key7       Key8       Location
---- ----       ----       ----       ----       ----       ----       ----       ----       --------
2019 Value 1235 Value 2345 Value 1231 Value 1235 Value 3536 Value 1231 Value 1234 Value 3624 Site6
2019 Value 2345 Value 2345 Value 1231 Value 1231 Value 3536 Value 1235 Value 1234 Value 1231 Site8
2019 Value 3536 Value 2345 Value 3536 Value 1235 Value 2345 Value 1235 Value 3536 Value 1235 Site7
2019 Value 2345 Value 1235 Value 1231 Value 1235 Value 1231 Value 3452 Value 1231 Value 2345 Site9
## Q:\Test\2019\08\26\SO_57659677_2.ps1
$hash = @{}
Import-Csv '.\Lookup_File.csv' -Delimiter '|' | ForEach {$Hash[$_.Key]=$_.Value}

$Master = Import-Csv '.\Master_File.csv' -Delimiter '|'
$ColNames = $Master[0].psobject.properties.name

ForEach($Item in $Master){
    For($i=1;$i -le 8;$i++){
       if($hash.ContainsKey($item."$($ColNames[$i])")){
           $item."$($ColNames[$i])" = $hash[$item."$($ColNames[$i])"]
       }
    }
}

$Master |ft -auto
> .\SO_57659677_2.ps1

Year Working    Approved   Rejected   WhatIf1    Whatif2    Submitted  Proposed   Syncd      Location
---- -------    --------   --------   -------    -------    ---------  --------   -----      --------
2019 Value 1235 Value 2345 Value 1231 Value 1235 Value 3536 Value 1231 Value 1234 Value 3624 Site6
2019 Value 2345 Value 2345 Value 1231 Value 1231 Value 3536 Value 1235 Value 1234 Value 1231 Site8
2019 Value 3536 Value 2345 Value 3536 Value 1235 Value 2345 Value 1235 Value 3536 Value 1235 Site7
2019 Value 2345 Value 1235 Value 1231 Value 1235 Value 1231 Value 3452 Value 1231 Value 2345 Site9