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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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_Powershell_Csv - Fatal编程技术网

Powershell 使用多个表解析CSV

Powershell 使用多个表解析CSV,powershell,csv,Powershell,Csv,我有一个包含多个表的CSV文件,例如: ##List 1## ID,Username,Department 1,John,HR 2,Barbara,Finance 3,Jane,IT ##List 2## ID,Username,CostCentre 1,Alex,3241 2,Jamie,3342 3,James,1234 4,Sarah,5536 有没有一种简单的方法可以让Powershell将其识别为两个表?我目前正在逐行阅读并使用 if($row -Like "*List*")

我有一个包含多个表的CSV文件,例如:

##List 1##
ID,Username,Department
1,John,HR
2,Barbara,Finance
3,Jane,IT

##List 2##
ID,Username,CostCentre
1,Alex,3241
2,Jamie,3342
3,James,1234
4,Sarah,5536
有没有一种简单的方法可以让Powershell将其识别为两个表?我目前正在逐行阅读并使用

if($row -Like "*List*") 
(以及其他一些逻辑)确定行的性质,然后将其写入两个文本文件中的一个,然后导入这些文本文件并再次迭代


但是有没有更有效的方法呢?

这还没有完全解决,我相信有人会想出更优雅的方法,但是它被分解成了几个步骤,所以可能会有助于解释一些PowerShell技术

$s=@"
##List 1##
ID,Username,Department
1,John,HR
2,Barbara,Finance
3,Jane,IT

##List 2##
ID,Username,CostCentre
1,Alex,3241
2,Jamie,3342
3,James,1234
4,Sarah,5536
"@

# break into individual lines
$s2 = ( $s -split "`r`n" ) 

# strip out blank lines
$s3 = $s2 | ? { $_ -ne "" }

# re-assemble into text blob
$s4 = $s3 -join "`r`n"

# split at tokens and skip the first, blank record
$s5 = $s4 -split "##List \d+##" | Select-Object -Skip 1

# remove blanks
$s6 = $s5 | ? { $_ -ne "" }

$s6 | % {

    # break into array of lines
    $s7 = $_ -split "`r`n"

    # remove blank lines
    $s8 = $s7 | ? { $_ -ne "" }

    $s8 | ConvertFrom-Csv

}