2013-12-10 103 views
0

我的文本文件,如下所示:批处理脚本解析与分隔txt文件为分号

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 

像制表符分隔

请建议

+0

这会输出到一个表格式,所以它的对齐?我可以轻松地搜索;并用空格替换。 – Chelseawillrecover

回答

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 
+0

这是目前为止我最喜欢的答案+ 1 – npocmaka

+0

当任何列值有空间时,这是失败 – dileepvarma

+0

@ user2215906:对不起,我的错。我忘了在命令中包含'token'子句。更新 –

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) 
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 
) 
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