How to get the last word in the last line in a file with Windows batch script?

橙三吉。 提交于 2020-01-05 03:57:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!