2011-11-16 111 views
0

內容文件A1的:文件A2批處理腳本合併兩個文件的行到第三個文件

AA 
VV 
BB 

內容:

DD 
EE 
FF 

我想要的A1A2內容合併爲在A3之下,因此A3的預期數據爲:

AADD 
VVEE 
BBFF 

另外,在A3預期的輸出可能是:

AA is from DD 
VV is from EE 
BB is from FF 

感謝您的幫助。在我發佈之前,我嘗試過搜索並找不到已發佈類似內容的人...

+1

這不是「將多個文件複製到一個文件」。它是「將兩個文件的行合併成第三個文件」。 –

回答

2

我們可以將文件的內容加載到批量變量數組所以每個其行可直接訪問任何你想:

@echo off 
setlocal EnableDelayedExpansion 

rem Load first file into A1 array: 
set i=0 
for /F "delims=" %%a in (A1.txt) do (
    set /A i+=1 
    set A1[!i!]=%%a 
) 

rem Load second file into A2 array: 
set i=0 
for /F "delims=" %%a in (A2.txt) do (
    set /A i+=1 
    set A2[!i!]=%%a 
) 

rem At this point, the number of lines is in %i% variable 

rem Merge data from both files and create the third one: 
for /L %%i in (1,1,%i%) do echo !A1[%%i]! is from !A2[%%i]!>> A3.txt 

編輯替代解決方案

還有另一種方法可以做到不使用批量變量,因此可以在任何大小的文件上使用,儘管速度較慢。我借用了Andy Morris在其解決方案中使用的方法:1-在兩個文件中插入行號,2-將兩個文件合併爲一個文件,3-對合並文件進行排序,以及4-將行合併到同一行中。下面的程序基本上是Andy的一個,有幾個小修改使其更快(修正了一個微妙的錯誤)。

@echo off 
setlocal EnableDelayedExpansion 

call :AddLineNumbers A1.txt A > Both.txt 
call :AddLineNumbers A2.txt B >> Both.txt 
sort Both.txt /O Sorted.txt 
echo EOF: >> Sorted.txt 
call :creatNewLines <Sorted.txt> Result.txt 
goto :eof 

:AddLineNumbers 
findstr /n ^^ %1 > tem.tmp 
for /f "tokens=1* delims=:" %%a in (tem.tmp) do (
    set /a lineNo=1000000+%%a 
    echo !lineNo!%2:%%b 
) 
goto :eof 

:creatNewLines 
set /p lineA1= 
for /f "tokens=1* delims=:" %%a in ("%lineA1%") do (
    if %%a == EOF goto :eof 
    set /p dummy=%%b< nul 
) 
set /p lineA2= 
for /f "tokens=1* delims=:" %%a in ("%lineA2%") do echo is from %%b 
goto creatNewLines 

根據其內容排序命令行。 Andy的原始方法可能會失敗,因爲在行號之後,根據行內容對行進行排序,因此每個文件的行可能會錯位。在這種方法中,在行號後添加一個附加字符(A或B),這樣每個文件的行總是放在正確的位置。

+0

這樣做的好方法,可能會讓大文件變得有趣。從http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true,所有變量的最大總環境變量大小,其中包括變量名稱和等號,爲65,536KB。但可能更快的速度達到此限制 –

+0

關於排序行的順序的好處。不知道我打算優化,但你有一些有趣的技術。 –

0

如果使用linux,我會推薦使用剪切和粘貼(命令行)。查看手冊頁。

或者,如果您不需要自動化,則可以使用vim塊模式剪切和粘貼。使用control-v進入塊模式可視模式。

1

如果你的原始數據是DATA1.TXT和Data2.txt這應該這樣做:

@echo off 

    call :AddLineNumbers data1.txt Tem1.txt 
    call :AddLineNumbers data2.txt Tem2.txt 

    copy tem1.txt + tem2.txt tem3.txt 
    sort <tem3.txt> tem4.txt 

    call :GetDataOut tem4.txt > tem5.txt 

    set OddData= 

    for /f %%a in (tem5.txt) do call :creatNewLines %%a 

goto :eof 



:AddLineNumbers 

    find /v /n "xx!!xx" < %1 > tem.txt 


    call :ProcessLines > %2 


goto :eof 

:ProcessLines 

    for /f "tokens=1,2 delims=[]" %%a in (tem.txt) do call :EachLine %%a %%b 

goto :eof 

:eachLine 

    set LineNo=00000%1 
    set data=%2 

    set LineNo=%LineNo:~-6% 

    echo %LineNo% %data% 

goto :eof 

:GetDataOut 
    for /f "tokens=2" %%a in (%1) do @echo %%a 
goto :eof 

:creatNewLines 
    if "%oddData%"=="" (
     set oddData=%1 
    ) else (
     echo %oddData% %1 
     set oddData= 
    ) 
goto :eof 
+0

你的方法有一個小錯誤。查看我答案中的最後一段... – Aacini

相關問題