问题
I have a file sample.txt
with the content as below.
H|20170331|CIS
A||A1140|GRM //header
A||A1140|GRM
A||A1140|GRM
A||A1140|GRM
A||A1140|GRM
T|20170331|5 //trailer
I need the last token in the last line i.e. 5
. (It's the count of the line except header trailer).
As of now, I tried below; it's giving the first token, i.e. T
, but I need the last token i.e. 5
.
@echo off
rem for /f "skip=1 tokens=2,* delims=" %%a in (sample.txt) do set "variable=%%a"
rem for /f "delims=|" %%x in (sample.txt) do set "variable=%%x"
for /f "delims=|" %%G in (sample.txt) do set "variable=%%G"
echo %variable%
pause
Note the file can contain thousands of records so the last token is required.
回答1:
Presuming you want to get rid of the //trailer
add a space as delimiter.
@Echo off
For /f "tokens=3delims=| " %%A in (sample.txt) Do Set LastRowCol3=%%A
Echo [LastRowCol3=%LastRowCol3%]
来源:https://stackoverflow.com/questions/43277485/how-to-get-the-last-word-in-the-last-line-in-a-file-with-windows-batch-script