簡體   English   中英

如何將文本文件的最后一行復制到另一個文本文件-批處理文件

[英]How can I copy the last line in a text file to another text file - batch file

我正在通過批處理文件運行curl並將輸出打印到文本文件。 我想將此文本文件的最后一行復制到另一個文件,這樣我將得到類似的內容:

第一個文件:

  0  594M    0 1017k    0     0   813k      0  0:12:27  0:00:01  0:12:26  813k
  0  594M    0 2735k    0     0  1215k      0  0:08:20  0:00:02  0:08:18 1215k
  0  594M    0 5074k    0     0  1561k      0  0:06:29  0:00:03  0:06:26 1561k
  1  594M    1 6716k    0     0  1580k      0  0:06:25  0:00:04  0:06:21 1580k
  1  594M    1 8027k    0     0  1489k      0  0:06:48  0:00:05  0:06:43 1566k
  1  594M    1 8438k    0     0  1350k      0  0:07:30  0:00:06  0:07:24 1484k
  1  594M    1 8883k    0     0  1225k      0  0:08:16  0:00:07  0:08:09 1229k
  1  594M    1 9555k    0     0  1158k      0  0:08:45  0:00:08  0:08:37  896k

第二個文件:

 1  594M    1 9555k    0     0  1158k      0  0:08:45  0:00:08  0:08:37  896k

批處理文件為:

curl -v -o NUL "http://...." 2>> file1.txt
timeout 10 /NOBREAK
copy /y file1.txt file1.tmp.txt 1>nul

setLocal EnableDelayedExpansion
    for /f "tokens=* delims=" %%a in (file1.tmp.txt) do (
    set var_file1=%%a  
    )
echo !var_file1!
echo !var_file1! > file2.txt

問題在於第一回顯行打印最后一行,而第二行將整個文件復制到文件2。

為了僅將file1的最后一行復制到file2,我應該更改什么?

謝謝!

我可以想到的唯一情況是代碼在何處給出您描述的結果,即所接收的文件(file1.txt)是否使用單回車符(\\ r,0x0D)作為行終止符。

如果使用\\ r作為行終止符時文件的長度總是<〜8190字節,並且文件永不包含! ,那么您可以使用以下代碼獲取最后一行:

@echo off
setlocal enableDelayedExpansion

curl -v -o NUL "http://...." 2>> file1.txt

:: I don't see why this line is here, but it shouldn't do any harm
timeout 10 /NOBREAK

:: Read each line and store in a variable - Only the last line is preserved
:: If the file uses CR as a line terminator, then the entire file is stored
:: Maximum variable length is ~8190 bytes
for /f delims^=^ eol^= %%A in (file1.txt) do set "s=%%A"

:: Define CR as a carriage return
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"

:: Define LF as a line feed
set ^"LF=^

^" This and the above empty line are critical - DO NOT REMOVE

:: The following is only needed if CR is the line terminator
:: But this does no harm if we already have the last line
for %%A in ("!CR!") do for %%B in ("!LF!") do (

  %= Remove existing LF =%
  set "s=!s:%%~B=!"

  %= Convert CR into LF =%
  set "s=!s:%%~A=%%~B!"
)

:: Get the last line for sure
for /f delims^=^ eol^= %%A in ("!s!") do >test2.txt set "s=%%A"

:: Write the output file
>file2.txt echo(!s!

無限制! 可以使用額外的代碼來消除文件中的錯誤,但是還有更好的方法。

使用我的JREPL.BAT正則表達式文本處理實用程序,可以提供一個強大而有效的解決方案。 JREPL.BAT是一個混合JScript /批處理腳本,可以從XP開始在任何Windows計算機上本地運行。

下面的代碼假定JREPL.BAT在當前文件夾中,或者更好的是在PATH列出的文件夾中。 它將與使用\\ r,\\ n,\\ r \\ n或\\ n \\ r作為行終止符的文件一起使用。

@echo off

curl -v -o NUL "http://...." 2>> file1.txt

:: I don't see why this line is here, but it shouldn't do any harm
timeout 10 /NOBREAK

call jrepl "([^\r\n]+)(\r\n|\n\r|\r|\n)?(?!.)" "$1" /m /jmatch /f file1.txt /o file2.txt

如果您不需要file1.txt,那么我相信您可以執行以下操作直接獲得結果

@echo off
curl -v -o NUL "http://...." 2>&1 | jrepl "([^\r\n]+)(\r\n|\n\r|\r|\n)?(?!.)" "$1" /m /jmatch /o file2.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM