2017-02-19 179 views
-1

我有一個文本文件。我必須交換奇數和偶數行。如何在文本文件中交換奇數行和偶數行?

我做了一個批處理腳本,將偶數行寫入testfile2.txt和奇數行寫入testfile3.txt

@echo off 
setlocal EnableDelayedExpansion 
set "filepath1=C:\\Users\\andyb\\Desktop\\testfile.txt" 
set "filepath2=C:\\Users\\andyb\\Desktop\\testfile2.txt" 
set "filepath3=C:\\Users\\andyb\\Desktop\\testfile3.txt" 
set counter=0 
set B=0 
for /F %%A in (%filepath1%) do (
set /a B=!counter!%%2 
if !B! equ 0 (echo %%A>>%filepath2%) else (echo %%A>>%filepath3%) 
set /A counter=counter+1 
) 

我想借此從文件1行包含奇數行,然後1號線從與偶數行的文件,並將其寫入我的第一個文件。但我不明白如何在FOR循環中執行此操作,因爲它只從一個文件讀取一行文件,而我無法在此循環中使用另一個文件。輸入文件的

實施例:

1a 
2b 
3c 
4d 

輸出文件的實施例:

2b 
1a 
4d 
3c 
+0

爲什麼每個文件路徑都有雙反斜槓?我曾經使用的每臺PC上的每個文件路徑都有一個反斜槓。 – Compo

+0

這是我的習慣,它的工作原理 – andybelous2

+3

[錯誤](https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247(v = vs.85).aspx#paths),更改你的壞習慣! – Compo

回答

0

有這個任務幾種解決方案。

第一個在交換奇數行和偶數行的批處理文件的所有行執行時使用延遲擴展。這意味着它不適用於行中帶有感嘆號的行,因爲!已從行中刪除。

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 
set "SourceFile=%USERPROFILE%\Desktop\TestFile.txt" 
if not exist "%SourceFile%" goto EndBatch 
set "TargetFile=%USERPROFILE%\Desktop\TestFile2.txt" 
del "%TargetFile%" 2>nul 

set "LineOdd=" 
for /F "usebackq delims=" %%I in ("%SourceFile%") do (
    if not defined LineOdd (
     set "LineOdd=%%I" 
    ) else (
     echo %%I>>"%TargetFile%" 
     echo !LineOdd!>>"%TargetFile%" 
     set "LineOdd=" 
    ) 
) 

if defined LineOdd echo !LineOdd!>>"%TargetFile%" 
move /Y "%TargetFile%" "%SourceFile%" 

:EndBatch 
endlocal 

空白和空行被FOR跳過,因此在目標文件丟失。並且以分號;開頭的行在讀取每行時被忽略FOR並且因此在輸出文件中也缺少該行。根據輸入的例子,這些限制在這裏不重要。

第一溶液的侷限性可以用本批處理代碼這是當然慢得多來避免:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 
set "SourceFile=%USERPROFILE%\Desktop\TestFile.txt" 
if not exist "%SourceFile%" goto EndBatch 
set "TargetFile=%USERPROFILE%\Desktop\TestFile2.txt" 
del "%TargetFile%" 2>nul 

set "LineOdd=" 
for /F "tokens=1* delims=:" %%H in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%SourceFile%"') do (
    if not defined LineOdd (
     set "LineOdd=_%%I" 
    ) else (
     if "%%I" == "" (
      echo/>>"%TargetFile%" 
     ) else (
      echo %%I>>"%TargetFile%" 
     ) 
     setlocal EnableDelayedExpansion 
     if "!LineOdd!" == "_" (
      echo/>>"%TargetFile%" 
     ) else (
      echo !LineOdd:~1!>>"%TargetFile%" 
     ) 
     endlocal 
     set "LineOdd=" 
    ) 
) 

if defined LineOdd (
    setlocal EnableDelayedExpansion 
    if "!LineOdd!" == "_" (
     echo/>>"%TargetFile%" 
    ) else (
     echo !LineOdd:~1!>>"%TargetFile%" 
    ) 
    endlocal 
) 
move /Y "%TargetFile%" "%SourceFile%" 

:EndBatch 
endlocal 

這將是還可以使用由Dave貝納姆書面混合批處理文件JREPL.BAT

call jrepl.bat "^(.*)\r\n(.*)\r\n" "$2\r\n$1\r\n" /M /X /F "%USERPROFILE%\Desktop\TestFile.txt" /O "%USERPROFILE%\Desktop\TestFile2.txt" 
move /Y "%USERPROFILE%\Desktop\TestFile2.txt" "%USERPROFILE%\Desktop\TestFile.txt" 

如果在使用此解決方案時是偶數行,則該文件的最後一行必須具有DOS/Windows行終止符(回車符\ r和換行符\ n)。

要了解使用的命令/可執行文件/批處理文件及其工作方式,請打開命令提示符窗口,在其中執行以下命令行,並仔細閱讀爲每個命令/可執行文件/批處理文件顯示的所有幫助頁面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr.exe /?
  • for /?
  • goto /?
  • if /?
  • jrepl.bat /?
  • move /?
  • set /?
  • setlocal /?

閱讀也是微軟的文章關於Using Command Redirection Operators2>nul>>的解釋。

0

下面的例子將來自testfile.txt,含有偶數行file0.out,和file1.out創建包含奇數行的兩個文件。

@Echo Off 
SetLocal EnableDelayedExpansion 
For /F "Tokens=1* Delims=:" %%A In ('FindStr/N "^" "testfile.txt"') Do (
    Set/A "_=%%A%%2" 
    (Echo(%%B)>>file!_!.out) 

根據您的要求重命名輸出文件。

+0

我需要將它結合到一個文件中的文件,從奇數行文件中取一行,從偶數行文件中取一行。 – andybelous2

+0

我需要在一個輸入文本文件中互換所有奇數和偶數文件 – andybelous2

+0

您的問題不清楚,它詢問奇數和偶數行,您現在問的是奇數和偶數文件。如果你沒有改善你的帖子以更好地解釋你的問題,它可能會被關閉,或繼續接收不滿意的答案。 – Compo

2

嘗試以下操作:

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 

rem // Define constants here: 
set "_FILE=textfile.txt" 

rem // Count number of lines: 
for /F %%C in ('^< "!_FILE!" find /C /V ""') do set "COUNT=%%C" 
rem // Divide by two, round up: 
set /A "COUNT=(COUNT+1)/2" 

< "!_FILE!" > "!_FILE!.tmp" (
    rem // Read files in blocks of two lines: 
    for /L %%I in (1,1,%COUNT%) do (
     set "LINE1=" & set "LINE2=" 
     set /P LINE1="" 
     set /P LINE2="" 
     echo(!LINE2! 
     echo(!LINE1! 
    ) 
) 

rem // Overwrite original file: 
> nul move /Y "!_FILE!.tmp" "!_FILE!" 

endlocal 
exit /B 
+0

不客氣!在文件包含奇數行的情況下,您希望最後一行發生什麼? – aschipfl

0

我認爲以相反順序重新傳遞奇數和偶數版本並不困難。追加到Compo的嘗試:

@Echo Off 
SetLocal EnableDelayedExpansion 
Set File=testfile 
For /F "Tokens=1* Delims=:" %%A In ('FindStr/N "^" "%File%.txt"' 
) Do Set/A "_=%%A%%2"&>>%File%_!_!.txt Echo(%%B 
<%File%_0.txt (For /f "delims=" %%A in (%File%_1.txt) Do (
    Set "B="&Set /P "B=" 
    Echo(!B! 
    Echo(%%A 
)) >%File%-new.txt 
Del %File%_* 

如果總數不均勻,倒數第二行將爲空。示例輸出:

2b 
1a 
4d 
3c 

5e 
相關問題