简体   繁体   中英

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. 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.

What should I change in order to copy only the last line of file1 to 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.

If the file is always <~8190 bytes long when it uses \\r as a line terminator, and the file never contains ! , 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 is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.

The code below assumes JREPL.BAT is in your current folder, or better yet, in a folder that is listed within your PATH. It will work with files that use \\r, \\n, \\r\\n, or \\n\\r as the line terminator.

@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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM