本文介绍了如何使用Windows批处理脚本获取文件最后一行中的最后一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件sample.txt,内容如下.

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

我需要最后一行中的最后一个令牌,即5. (这是标题标头行除外的行数).

I need the last token in the last line i.e. 5. (It's the count of the line except header trailer).

到目前为止,我尝试过以下方法;它给出了第一个令牌,即T,但是我需要最后一个令牌,即5.

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.

推荐答案

假设您想摆脱//trailer,请添加一个空格作为定界符.

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%]

这篇关于如何使用Windows批处理脚本获取文件最后一行中的最后一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 02:23