You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在批处理中替换文本文件中的竖线(|)字符?

批处理中替换竖线(|)字符的解决方案

问题原因

你的PowerShell命令出现异常输出,是因为|是正则表达式的特殊元字符(代表“逻辑或”),直接使用'|'作为匹配字符串时,PowerShell会将其解析为匹配任意单个字符,导致每个字符之间都被插入空格。

修正后的PowerShell命令

以下两种方式都能正确识别并替换竖线字符:

  • 使用转义符处理正则元字符:
    powershell -Command "(gc output.txt) -replace '\|', ' ' | Out-File -encoding ASCII output.txt"
    
  • 将竖线放入字符集[]中(字符集内的元字符会被视为普通字符):
    powershell -Command "(gc output.txt) -replace '[|]', ' ' | Out-File -encoding ASCII output.txt"
    

执行上述命令后,输入内容80853||OHNED|Mira会被替换为期望的80853 OHNED Mira。

纯批处理实现方案(不依赖PowerShell)

如果需要直接用批处理脚本完成替换,可使用以下代码:

@echo off
setlocal enabledelayedexpansion
set "input=output.txt"
set "temp=temp.txt"
del "%temp%" 2>nul
for /f "delims=" %%a in (%input%) do (
    set "line=%%a"
    set "line=!line:|= !"
    echo !line!>>%temp%
)
move /y "%temp%" "%input%" >nul
endlocal

该脚本会逐行读取目标文件,将每行中的|替换为空格,最后用临时文件覆盖原文件。

内容的提问来源于stack exchange,提问作者moveit124

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:05:16