简体   繁体   English

如何在Powershell中的csv文件中循环通过一行

[英]how do I loop through a line from a csv file in powershell

I want to process a csv file in powershell, but I don't know what the column headings in the CSV file will be when it is processed. 我想在powershell中处理一个csv文件,但是我不知道处理该文件时CSV文件中的列标题是什么。

For example: 例如:

$path = "d:\scratch\export.csv"
$csv = Import-csv -path $path

foreach($line in $csv)
{ 
    foreach ($head in $line | get-member | where-object {$_.MemberType -eq "NoteProperty"} | select Definition)
    {
        #pseudocode...
        doSomething($head.columnName, $head.value)
    }

}

How do I loop through the line in the csv file, getting the name of the column and the value? 如何遍历csv文件中的行,获取列的名称和值? Or is there another way I should be doing this (like not using Import-csv)? 还是有另一种方式我应该这样做(例如不使用Import-csv)?

Import-Csv $path | Foreach-Object { 

    foreach ($property in $_.PSObject.Properties)
    {
        doSomething $property.Name, $property.Value
    } 

}

A slightly other way of iterating through each column of each line of a CSV-file would be 遍历CSV文件每一行每一列的另一种方式是

$path = "d:\scratch\export.csv"
$csv = Import-Csv -path $path

foreach($line in $csv)
{ 
    $properties = $line | Get-Member -MemberType Properties
    for($i=0; $i -lt $properties.Count;$i++)
    {
        $column = $properties[$i]
        $columnvalue = $line | Select -ExpandProperty $column.Name

        # doSomething $column.Name $columnvalue 
        # doSomething $i $columnvalue 
    }
} 

so you have the choice: you can use either $column.Name to get the name of the column, or $i to get the number of the column 因此您可以选择:您可以使用$column.Name获取列的名称,也可以使用$i获取列的编号

$header3 = @("Field_1","Field_2","Field_3","Field_4","Field_5")     

Import-Csv $fileName -Header $header3 -Delimiter "`t" | select -skip 3 | Foreach-Object {

    $record = $indexName 
    foreach ($property in $_.PSObject.Properties){

        #doSomething $property.Name, $property.Value

            if($property.Name -like '*TextWrittenAsNumber*'){

                $record = $record + "," + '"' + $property.Value + '"' 
            }
            else{
                $record = $record + "," + $property.Value 
            }                           
    }               

        $array.add($record) | out-null  
        #write-host $record                         
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用批处理或PowerShell脚本循环访问CSV文件? - How do I loop through a CSV file with a batch or PowerShell script? foreach 循环通过 powershell 中的 csv 文件中的一行不起作用 - foreach loop through a line from a csv file in powershell not working 在 PowerShell 中,我如何循环遍历 CSV 文件并根据 csv 中的键值创建摘要哈希表 - in PowerShell how do I Loop through CSV file and create a summary hashtable based on key value in csv 如何从powershell中的文本文件中删除一行? - How do I delete a line from a text file in powershell? 如何从 CSV 文件中遍历层次结构? - How to loop through hierarchy from CSV file? 如何分别针对一组固定的字符循环从文件中读取的字符串,如果它们匹配,则在 powershell 中打印该字符串 - How do I loop through a string read from a file against a fixed set of characters individually and if they match print the string in powershell 如何在没有“#TYPE”行的情况下使用PowerShell输出CSV? - How do I output a CSV using PowerShell without the “#TYPE” line? 如何使用Powershell导出csv文件的每一行 - How can I export each line of a csv file with powershell 如何从.CSV单元格使用Powershell写入.cmd文件的新行 - How to write to new line of .cmd file with Powershell from .CSV cell 如何使用PowerShell导入CSV并从文本文件中读取一行? - How to Import a CSV and read a line from text file using PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM