2013-12-10 133 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