繁体   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