读取一个文件,计算定界符和输出不匹配定界符的行号
Read a file, count delimiters and output line number with mismatched delimiter
我有一个名为:test_file.txt 的文件。第二行有 4 个竖线分隔符,除第二行外的所有其他行都有 3 个竖线分隔符。
我只想输出第 2 行,因为它比其他行多了一个定界符。
$colCnt = "C:\test.txt"
[int]$LastSplitCount = $Null
Get-Content $colCnt | ?{$_} | Select -Skip 1 | %{if($LastSplitCount -and !
($_.split("|").Count -eq $LastSplitCount))
{"Process stopped at line number $($_.psobject.Properties.value[5]) for column count mis-match.";break}
elseif(!$LastSplitCount){$LastSplitCount = $_.split("|").Count}}
如果您的文本文件看起来像这样:
blah|using|three|delimiters
blah|using|four |delimiter |characters
blah|using|three|delimiters
blah|using|four |delimiter |characters
blah|using two |delimiters
以下代码应输出多于(或少于)3 个 |
个分隔符的行:
$line = 0
switch -Regex -File "C:\test.txt" {
'^(?:[^|]*\|){3}[^|]*$' { $line++ } # this line is OK, just increase the line counter
default { "Bad delimiter count in line {0}: '{1}'" -f ++$line, $_ }
}
输出:
Bad delimiter count in line 2: 'blah|using|four |delimiter |characters'
Bad delimiter count in line 4: 'blah|using|four |delimiter |characters'
Bad delimiter count in line 5: 'blah|using two |delimiters'
正则表达式详细信息:
^ Assert position at the beginning of the string
(?: Match the regular expression below
[^|] Match any character that is NOT a “|”
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\| Match the character “|” literally
){3} Exactly 3 times
[^|] Match any character that is NOT a “|”
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ Assert position at the end of the string (or before the line break at the end of the string, if any)
我有一个名为:test_file.txt 的文件。第二行有 4 个竖线分隔符,除第二行外的所有其他行都有 3 个竖线分隔符。 我只想输出第 2 行,因为它比其他行多了一个定界符。
$colCnt = "C:\test.txt"
[int]$LastSplitCount = $Null
Get-Content $colCnt | ?{$_} | Select -Skip 1 | %{if($LastSplitCount -and !
($_.split("|").Count -eq $LastSplitCount))
{"Process stopped at line number $($_.psobject.Properties.value[5]) for column count mis-match.";break}
elseif(!$LastSplitCount){$LastSplitCount = $_.split("|").Count}}
如果您的文本文件看起来像这样:
blah|using|three|delimiters blah|using|four |delimiter |characters blah|using|three|delimiters blah|using|four |delimiter |characters blah|using two |delimiters
以下代码应输出多于(或少于)3 个 |
个分隔符的行:
$line = 0
switch -Regex -File "C:\test.txt" {
'^(?:[^|]*\|){3}[^|]*$' { $line++ } # this line is OK, just increase the line counter
default { "Bad delimiter count in line {0}: '{1}'" -f ++$line, $_ }
}
输出:
Bad delimiter count in line 2: 'blah|using|four |delimiter |characters' Bad delimiter count in line 4: 'blah|using|four |delimiter |characters' Bad delimiter count in line 5: 'blah|using two |delimiters'
正则表达式详细信息:
^ Assert position at the beginning of the string (?: Match the regular expression below [^|] Match any character that is NOT a “|” * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) \| Match the character “|” literally ){3} Exactly 3 times [^|] Match any character that is NOT a “|” * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) $ Assert position at the end of the string (or before the line break at the end of the string, if any)