Powershell 2.0用于计算文件中的列数

Powershell 2.0用于计算文件中的列数,powershell,csv,powershell-2.0,Powershell,Csv,Powershell 2.0,我需要检查文本文件,确定列号是否不匹配。如果有,则应显示错误消息 我找到了下面的解决方案,因为分隔符是“,”: 代码如下: $colCnt=“12_06_2019.txt” [int]$LastSplitCount=$Null 获取内容$colCnt| %{ if($LastSplitCount-and!($u.split(“,”).Count-eq$LastSplitCount)) { “进程在行号处停止$( 列计数不匹配的$\ pObject.Properties.value[5]) 打破

我需要检查文本文件,确定列号是否不匹配。如果有,则应显示错误消息

我找到了下面的解决方案,因为分隔符是“,”:

代码如下:

$colCnt=“12_06_2019.txt”
[int]$LastSplitCount=$Null
获取内容$colCnt|
%{
if($LastSplitCount-and!($u.split(“,”).Count-eq$LastSplitCount))
{
“进程在行号处停止$(
列计数不匹配的$\ pObject.Properties.value[5])
打破
}
elseif(!$LastSplitCount)
{
$LastSplitCount=$\分割(“,”.Count}
}
如果使用powershell 5.1,此功能正常工作,但在2.0版中运行时遇到错误消息。错误消息:

"Cannot index into a null array. At line:1 char:30 + $_.psobject.Properties.value[<<<< 5].`
“无法索引到空数组中。在第1行char:30+$\.psobject.Properties.value[尝试以下操作:

$file        = "12_06_2019.txt"
$delimiter   = ','
$oldCols     = -1
$lineCounter = 0

foreach( $line in [System.IO.File]::ReadLines($file) )
{
    $lineCounter++
    $cols = @($line -split $delimiter).Count

    if( $oldCols -eq -1 ) {
        $oldCols = $cols
    }
    elseif( $oldCols -ne $cols ) {
        "Column count mis-match at line $($lineCounter)"
    }
}
试试这个:

$file        = "12_06_2019.txt"
$delimiter   = ','
$oldCols     = -1
$lineCounter = 0

foreach( $line in [System.IO.File]::ReadLines($file) )
{
    $lineCounter++
    $cols = @($line -split $delimiter).Count

    if( $oldCols -eq -1 ) {
        $oldCols = $cols
    }
    elseif( $oldCols -ne $cols ) {
        "Column count mis-match at line $($lineCounter)"
    }
}

$.psobject.Properties.value[5]
-->($\.psobject.Properties)[5]。value
感谢管理员。这很有效。谢谢你的朋友!:)
$.psobject.Properties.value[5]
-->($\.psobject.Properties)[5]。value感谢管理员。这很有效。谢谢朋友!:)