PowerShell隔行合并文本问题及代码调试求助
PowerShell 合并每行与下一行的问题
需求
需要将文本文件中每行与紧接着的下一行合并为一行,保留原有效空格,仅去除两行间的换行符。
输入样例
ALPHA-FETOPROTEIN ROUTINE CH 0203 001 02/03/2023@10:45 LIVERF3 ###-##-#### #######,#### In lab ALPHA-FETOPROTEIN ROUTINE CH 0203 234 02/03/2023@11:05 LIVER ###-##-#### ########,######## In lab ANION GAP STAT CH 0203 124 02/03/2023@11:06 DAY ###-##-#### ######,##### #### In lab BASIC METABOLIC PANE ROUTINE CH 0203 001 02/03/2023@10:45 LIVERF3 ###-##-#### #######,#### ###### In lab
期望输出样例
ALPHA-FETOPROTEIN ROUTINE CH 0203 001 02/03/2023@10:45 LIVERF3 ###-##-#### #######,#### In lab ALPHA-FETOPROTEIN ROUTINE CH 0203 234 02/03/2023@11:05 LIVER ###-##-#### ########,######## In lab ANION GAP STAT CH 0203 124 02/03/2023@11:06 DAY ###-##-#### ######,##### #### In lab BASIC METABOLIC PANE ROUTINE CH 0203 001 02/03/2023@10:45 LIVERF3 ###-##-#### #######,#### ###### In lab
尝试过的无效代码
for($i = 0; $i -lt $splitLines.Count; $i += 2){ $splitLines[$i,($i+1)] -join ' ' }
完整未生效脚本
# SET VARIABLES $inputfile = "C:\Users\Will\Desktop\testfile.txt" $outputfile = "C:\Users\Will\Desktop\testfileformatted.txt" $new_output = "C:\Users\Will\Desktop\new_formatted.txt" # REMOVE EXTRA CHARACTERS $remove_beginning_capture = "-------------------------------------------------------------------------------" $remove_end_capture = "===============================================================================" $remove_line = "------" $remove_strings_with_spaces = " \d" Get-Content $inputfile | Where-Object {$_ -notmatch $remove_beginning_capture} | Where-Object {$_ -notmatch $remove_end_capture} | Where-Object {$_ -notmatch $remove_line} | Where-Object {$_ -notmatch $remove_strings_with_spaces} | ? {$_.trim() -ne "" } | Set-Content $outputfile # Measures line length for loop $file_lines = gc $outputfile | Measure-Object #Remove Whitespace # $whitespace_removed = (Get-Content $outputfile -Raw) -replace '\s+', ' '| Set-Content -Path C:\Users\Will\Desktop\new_formatted.csv # Combine every other line $lines = Get-Content $outputfile -Raw $newcontent = $lines.Replace("`n"," ") Write-Host "Content: $newcontent" $newcontent | Set-Content $new_output for($i = 0; $i -lt $splitLines.Count; $i += 2){ $splitLines[$i,($i+1)] -join ' ' }
解决方法
问题分析
现有脚本的核心问题:
- 变量
$splitLines未定义,导致合并循环完全不执行 - 用
Replace("n"," ")`会把所有换行符替换为空格,导致所有行连成一串,不符合“每两行合并”的需求
修改后完整脚本
# SET VARIABLES $inputfile = "C:\Users\Will\Desktop\testfile.txt" $outputfile = "C:\Users\Will\Desktop\testfileformatted.txt" $new_output = "C:\Users\Will\Desktop\new_formatted.txt" # REMOVE EXTRA CHARACTERS $remove_beginning_capture = "-------------------------------------------------------------------------------" $remove_end_capture = "===============================================================================" $remove_line = "------" $remove_strings_with_spaces = " \d" Get-Content $inputfile | Where-Object {$_ -notmatch $remove_beginning_capture} | Where-Object {$_ -notmatch $remove_end_capture} | Where-Object {$_ -notmatch $remove_line} | Where-Object {$_ -notmatch $remove_strings_with_spaces} | Where-Object {$_.trim() -ne "" } | Set-Content $outputfile # 读取处理后的文件行(按行获取数组) $splitLines = Get-Content $outputfile # 合并每两行 $mergedLines = @() for($i = 0; $i -lt $splitLines.Count; $i += 2){ # 避免行数为奇数时越界 if ($i + 1 -lt $splitLines.Count) { # 合并当前行与下一行,去除下一行前导冗余空格,用单个空格连接 $mergedLine = "$($splitLines[$i]) $($splitLines[$i+1].TrimStart())" } else { $mergedLine = $splitLines[$i] } $mergedLines += $mergedLine } # 输出到目标文件 $mergedLines | Set-Content $new_output
关键说明
- 正确读取行数据:用
Get-Content $outputfile直接获取行数组并赋值给$splitLines,确保循环有可用的行集合 - 合并逻辑优化:
- 循环步长设为2,每次处理第
i和i+1行 - 用
TrimStart()去除第二行开头的冗余空格,匹配期望输出的格式 - 增加越界判断,防止文件行数为奇数时出现错误
- 循环步长设为2,每次处理第
- 结果输出:将合并后的行数组写入目标文件,保留每行独立的换行符
内容的提问来源于stack exchange,提问作者Baby Yoda
相关产品推荐
相关产品推荐

