2017-01-30 39 views
0

我正在读取一个由空白行分隔的CSV文件。我想要在数组中捕获空白行之间的每个部分。数组将如下所示。在PowerShell中构建散列阵列

array[section0][row0] 
array[section0][row1] 
array[section0][row2] 
array[section1][row0] 
array[section1][row1] 
array[section1][row2] 

CSV文件格式与以下类似。

this,is,section,one,line,one 
this,is,section,one,line,two 
,,,,,,,,,,,,,,,,,,,,, 
this,is,section,two,line,one 
this,is,section,two,line,two 
this,is,section,two,line,three 
section,two,with,extra,commas,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
this,is,section,three,line,one 
this,is,section,three,line,two 
this,is,section,three,line,three 
section,three,with,extra,commas,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
this,is,section,four,line,one 
this,is,section,four,line,two 
this,is,section,four,line,three 
section,four,with,extra,commas,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,, 
,,,,,,,,,,,,,,,,,,,,,

我试图将数组取到$section[sectionnumber][sectionrownumber]。但这是失败的。

我得到的错误说:“索引超出界限”“不能索引空阵”

我相信它与数组初始化有关。我只是无法得到这个工作。

$file = "filename" 
$path = Split-Path $file 
$import = Get-Content $file 

#find blank rows 
$r = 0 
$blank = "yes" 
$firstblank = "yes" 
$sectionnumber = 0 
#initialize section array 
$section = ,@() 
foreach ($row in $import) { 
    if ($row -ne ",,,,,,,,,,,,,,,,,,,,,") { 
     #not a blank row 
     if ($firstblank -eq "yes") { 
      $blank = "no" 
      $firstblank = "no" 
     } elseif($blank -eq "yes") { 
      $blank = "no" 
      $r++ 
     } else {} 
     #initialize array for multidemension 
     $section[$r][$sectionnumber] = $row 
     $sectionnumber++ 
    } else { 
     #this is a blank row 
     if ($blank = "no") { 
      $blank = "yes" 
      $sectionnumber = 0 
     } else { 
      $blank = "yes" 
      $sectionnumber = 0 
     } 
    } 
} 
Write-Host $section 

回答

0

如果你允许,PowerShell可以为你完成大部分繁重工作。意思就是你可能想要做的。

$import = Get-Content 'C:\path\to\your.txt' -Raw 

$section = [ordered]@{} 
$i = 0 

# remove trailing consecutive commas from the end of each line, then 
# split the lines at consecutive line breaks 
$import -replace '(?m),+$' -replace "`r?`n", "`n" -split "`n`n+" | Where-Object { 
    # filter out blank lines 
    $_.Trim() 
} | ForEach-Object { 
    # for each text block create a new section with a nested hashtable, 
    # then split each block into individual rows 
    $j = 0 
    $section["section$i"] = [ordered]@{} 
    $_ -split "`n" | ForEach-Object { 
     # split each row at commas and assign to a new record in the 
     # nested hashtable 
     $section["section$i"]["row$j"] = $_ -split ',' 
     $j++ 
    } 
    $i++ 
} 

请注意,你需要的PowerShell v3的或更新的有序哈希表和Get-Content -Raw。如果仅限于PowerShell v2或更早版本,请删除[ordered]类型转换并将参数-Raw替换为| Out-String


编辑:如果你想行的只是一个简单的列表,其中每行被换成了一节和行标题您可以简化上述这样的:

$import -replace '(?m),+$' -replace "`r?`n", "`n" -split "`n`n+" | Where-Object { 
    $_.Trim() 
} | ForEach-Object { 
    $j = 0 
    $_ -split "`n" | ForEach-Object { 
     "section $i - row $j - $_" 
     $j++ 
    } 
    $i++ 
} 
+0

这是有帮助的,但不完全是我期望实现的。我正在努力写出一个很好的回应。但是,“进入”试图在这里找到一个新的线提交表格。所以我对此评论无法做太多的工作以显示更多信息。你知道如何防止自动提交这个评论,当我按Enter键? –

+0

我发布了更多信息作为答案,因为我无法弄清楚如何做它作为评论 –