2014-07-07 36 views
0

我已經使用repl.bat從文本文件中提取了幾個月的某些行。不幸的是,輸出已經改爲XML,並且它現在在XML標籤中使用了反斜槓作爲分隔符。使用repl.bat從XML中提取文本爲文本

下面是我用什麼當輸出文件是這樣的

Job Notes=John Smith 123456 dd/MM/yyyy h:mm:ss PM 654321 
File Type=4 
Location=3 

代碼

@echo off 
set "input=before.txt" 
set "output=after.txt" 
findstr /r /i /c:"^Job Notes=" "%input%" |repl ".*=(.*) (\d+) (\d+\/\d+\/\d+) \d+:\d+:\d+ .*" "Name=$1\r\nFile Number=$2\r\nDate=$3" x >"%output%" 
findstr /r /i /c:"^File Type=" "%input%" >>"%output%" 
findstr /r /i /c:"^Location=" "%input%" >>"%output%" 

的輸出中現在是XML

<job_notes>John Smith\123456\dd/MM/yyyy h:mm:ss PM\654321</job_notes> 
<file_type>4</file_type> 
<location>3</location> 

不知道這是否是問題但XML的結構是

<root> 
    <job> 
     <job_notes>xxxxxxx</job_notes> 
     <file_type>x</file_type> 
     <location>x</location> 
    </job> 
</root> 

無法獲取修改後的腳本。不確定問題是XML文件的結構還是分隔符。

由於

回答

0

使用repl.bat的適於批處理文件寫入由dbenham

@echo off 
set "input=before.xml" 
set "output=after.txt" 
findstr.exe /r /i /c:"<job_notes>" "%input%" | repl.bat ".*job_notes.(.*)\\(\d+).(\d+\/\d+\/\d+).*" "Name=$1\r\nFile Number=$2\r\nDate=$3" x >"%output%" 
findstr.exe /r /i /c:"<file_type>" "%input%" | repl.bat ".*file_type.(\d+).*" "File Type=$1" x >>"%output%" 
findstr.exe /r /i /c:"<location>" "%input%" | repl.bat ".*location.(\d+).*" "Location=$1" x >>"%output%" 

我用於與下列數據測試before.xml

<root> 
    <job> 
     <job_notes>John Smith\123456\02/06/2014 8:34:27 PM\654321</job_notes> 
     <file_type>4</file_type> 
     <location>3</location> 
    </job> 
</root> 

文件after.txt包含此輸入例如批處理文件執行後:

Name=John Smith 
File Number=123456 
Date=02/06/2014 
File Type=4 
Location=3