[英]How can I copy the last line in a text file to another text file - batch file
I'm running curl through a batch file and print the output to a text file. 我正在通过批处理文件运行curl并将输出打印到文本文件。 I want to copy the last line of this text file to another file so i'll have something like: 我想将此文本文件的最后一行复制到另一个文件,这样我将得到类似的内容:
The first file: 第一个文件:
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
The second file: 第二个文件:
1 594M 1 9555k 0 0 1158k 0 0:08:45 0:00:08 0:08:37 896k
The batch file is: 批处理文件为:
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
The problem is that the first echo line prints the last line but the second line copy the entire file1 to file2. 问题在于第一回显行打印最后一行,而第二行将整个文件复制到文件2。
What should I change in order to copy only the last line of file1 to file2 ? 为了仅将file1的最后一行复制到file2,我应该更改什么?
Thanks! 谢谢!
The only scenario I can think of where your code gives the results you describe is if the file you are receiving (file1.txt) uses a single carriage return (\\r, 0x0D) as a line terminator. 我可以想到的唯一情况是代码在何处给出您描述的结果,即所接收的文件(file1.txt)是否使用单回车符(\\ r,0x0D)作为行终止符。
If the file is always <~8190 bytes long when it uses \\r as a line terminator, and the file never contains !
如果使用\\ r作为行终止符时文件的长度总是<〜8190字节,并且文件永不包含!
, then you could use the following code to get the last line: ,那么您可以使用以下代码获取最后一行:
@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!
The limitation of no !
无限制!
in the file can be eliminated with extra code, but there is a much better way. 可以使用额外的代码来消除文件中的错误,但是还有更好的方法。
A robust and efficient solution is available using my JREPL.BAT regular expression text processing utility . 使用我的JREPL.BAT正则表达式文本处理实用程序,可以提供一个强大而有效的解决方案。 JREPL.BAT is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. JREPL.BAT是一个混合JScript /批处理脚本,可以从XP开始在任何Windows计算机上本地运行。
The code below assumes JREPL.BAT is in your current folder, or better yet, in a folder that is listed within your PATH. 下面的代码假定JREPL.BAT在当前文件夹中,或者更好的是在PATH列出的文件夹中。 It will work with files that use \\r, \\n, \\r\\n, or \\n\\r as the line terminator. 它将与使用\\ 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
If you don't need file1.txt, then I believe you could do the following to get the result directly 如果您不需要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.