批处理文件从文本文件的标题替换下划线(_)

时间:2014-02-27 11:30:36

标签: batch-file batch-processing

我有一个Pipe(|)分隔文件 new.txt 。我想创建一个批处理文件,用来替换 new.txt 中的下划线字符,保持数据不变。我创建了一个批处理,甚至可以从数据中替换下划线。请帮忙。

new.txt包含标题

Transaction_Type|Spend_Source_System|Event_ID|Expense_ID

我写的批处理代码是

    @echo off &setlocal
set "search=_"
set "replace= "
set "textfile=D:\test.txt"
set "newfile=D:\test2.txt"
(for /f %%i in ('findstr  "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%"

1 个答案:

答案 0 :(得分:0)

(for /f "DELIMS=" %%i in ('findstr  "^" "%textfile%"') do (

应该解决你的问题。


啊 - 看起来他们可能希望替换标题行中的下划线

set "search=_"
set "replace= "
set "textfile=D:\test.txt"
set "newfile=D:\test2.txt"
SET header=Y
(for /f "delims=" %%i in ('findstr  "^" "%textfile%"') do (
  IF DEFINED header (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    ENDLOCAL
    SET "header="
  ) ELSE (ECHO(%%i)
))>"%newfile%"