本帖最后由 newswan 于 2021-6-5 14:49 编辑
- $file = "asdf22.txt"
-
- [System.Collections.ArrayList]$fileC = get-content -path $file
-
- for ( $i = 0 ; $i -lt $fileC.count -1 ; $i += 1 )
- {
- if ( $fileC[$i] -match "#" )
- {
- while ( $fileC[$i+1] -match "#" )
- {
- $fileC[$i] = $fileC[$i] + "," + $fileC[$i+1]
- $fileC.removeat($i+1)
- }
- }
- }
-
- for ( $i = 0 ; $i -lt $fileC.count ; $i += 1 )
- {
- if ( $fileC[$i] -match "#" )
- {
- $m = select-string "(?<=#)[^,]+(?=,|$)" -allmatches -inputobject $fileC[$i]
- $fileC[$i] = $m.matches.value -join ","
- }
- }
-
- $fileC
复制代码 powershell 处理包含#的行,思路更简单,先合并包含#相邻的行,再处理带#的行 |