批处理脚本用分隔符作为分号解析文本文件

时间:2013-12-10 10:36:08

标签: windows batch-file

我的文本文件如下所示:

H;SEQUENCENUMMER;TIMESTAMP;VERSION;VALIDITY_FLAG;SID
D;1077;1383519656;"20.0.8-2";1;;
D;1079;1383519657;"20.0.8-2";2;;

我想用Windows批处理程序解析这个,输出文件看起来像下面那样:

H  SEQUENCENUMMER  TIMESTAMP  VERSION  VALIDITY_FLAG  SID
D  1077  1383519656  "20.0.8-2"  1
D  1078  1383519657  "20.0.8-3"  2

类似于制表符分隔的

建议

4 个答案:

答案 0 :(得分:5)

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem Get a tab character
    for /f tokens^=^*^ delims^= %%t in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do set "tab=%%t" 

    rem For each line in text file, replace ; with a tab    
    (for /f "tokens=*" %%l in (plaintext.txt) do (
        set "line=%%l"  
        echo !line:;=%tab%!
    )) > output.txt

    endlocal

答案 1 :(得分:1)

@echo off
setlocal enabledelayedexpansion
if exist output.txt del output.txt
for /f "delims=" %%a in ('type "Your Text File.txt"') do (set $line=%%a
                                               echo !$line:;=   !>>output.txt)

答案 2 :(得分:0)

您可以使用嵌套的for循环,如下所示:

@echo off
echo.|set a=>outfile.txt
for /f %%A in (infile.txt) do (
    for %%B in (%%A) do (
        echo.|set /P ="%%B    ">>outfile.txt
    )
    echo.>>outfile.txt
)

答案 3 :(得分:0)

你似乎并没有解析任何东西,你只是用标签替换分号。您可以使用VBScript执行此操作。将以下内容另存为“process.vbs”

Option Explicit
Dim Line,Regex

Set RegEx = new RegExp
Regex.Pattern = ";"
Regex.Global=True

Do While Not WScript.StdIn.AtEndOfStream
   Line = WScript.StdIn.ReadLine()
   Line = Regex.Replace(Line,VBTab)
   WScript.Echo Line
Loop

然后你可以像这样运行它:

vbscript /nologo process.vbs < yourfile > newfile