简体   繁体   English

批次:找到以字符开头的最后一行,然后从那里开始阅读

[英]Batch: find last line that starts with character then start reading from there

Good day, I want to know how do I create a batch script that searches through a text file that contains multiple lines that start with *============== . 美好的一天,我想知道如何创建一个批处理脚本,以搜索文本文件,该文件包含以*==============开头的多行。 I wish for it to find the last occurrence of a line like this and then only use everything thereafter in the rest of the script. 我希望它能找到最后一行这样的行,然后在脚本的其余部分中仅使用此后的所有内容。

Currently I use for /F "tokens=*" %%A in (%file%.txt) do ( [process] %%A ) to scan in each line. 目前,我for /F "tokens=*" %%A in (%file%.txt) do ( [process] %%A )使用for /F "tokens=*" %%A in (%file%.txt) do ( [process] %%A )来进行扫描for /F "tokens=*" %%A in (%file%.txt) do ( [process] %%A )

Any ideas? 有任何想法吗?

Thanks 谢谢

You could scan the file for this line (as you are doing it now) but instead of processing it directly you could store the lines below *============== somehow (eg. in a temp file). 您可以扫描文件中的这一行(如您现在所做的那样),但是可以直接将其存储在*==============下面的行中(例如,以临时方式)文件)。 If you find another line like *============== you delete everything you have saved till then and start saving the lines below again. 如果您发现另一行,例如*============== ,则删除之前保存的所有内容,然后再次开始保存下面的行。 You will end up at the last line containing *============== save everything below it and reach the end of file so only the last block will remain in the temp file. 您将结束于包含*==============的最后一行,将其下面的所有内容保存并到达文件末尾,因此只有最后一块将保留在临时文件中。 Now you can process the file and be sure that it contains only data below the last *============== line. 现在,您可以处理文件,并确保它仅包含最后*==============行以下的数据。

The code would look something like this: 代码看起来像这样:

SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (%file%.txt) do (
    SET currentline = %%A
    SET currentstring=!currentstring~0,15!
    IF !currentstring!==*============== (
        TYPE NUL > tempFile.txt
    ) ELSE (
        ECHO %%A>>tempFile.txt
    )
)
::process the data from tempFile.txt

You may use findstr command to find the lines that start with *============== and get the number of the last one, and use such number in skip option of for command: 您可以使用findstr命令查找以*==============开头的行,并获取最后一个的行,然后在for命令的skip选项中使用该行:

rem Get the line number of the last line that start with "*=============="
for /F "delims=:" %%a in ('findstr /N "^\*==============" %file%.txt') do set lastLine=%%a

rem Process the file from the next line to that one on
for /F "skip=%lastLine% tokens=*" %%A in (%file%.txt) do ( [process] %%A )

I came across this answer whilst searching for a solution to effectively the same problem. 我在寻找有效解决同一问题的解决方案时遇到了这个答案。 MichaelS's answer above got me on the right track; MichaelS的上述回答使我步入正轨; there were just a few typos in that stopped it working however. 但是只有少数错别字使它无法正常工作。 For future searchers, this is a working version: 对于将来的搜索者,这是一个工作版本:

SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (%file%.txt) do (
    SET currentline=%%A
    SET currentstring=!currentline:~0,15!
    IF !currentstring!==*============== (
        TYPE NUL>tempFile.txt
    ) ELSE (
        ECHO %%A>>tempFile.txt
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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