简体   繁体   中英

Find last occurence of a string using findstr on windows command line

I have a text file with a string occurs multiple lines. I want to get the line where this is the last occurrence of the string. I could find all occurrences using below command but I want only last occurrence in single line command.

findstr /C:"Apple Ball Cat" Book.txt

Can someone guide me if it is possible?

FOR command from command line:

(for /F "delims=" %G in ('findstr /C:"Apple Ball Cat" Book.txt') do @set "lastoccur=%G")&set lastoccur

From a batch script:

@echo off
set "lastoccur="
for /F "delims=" %%G in ('findstr /C:"Apple Ball Cat" Book.txt') do set "lastoccur=%%G"
set lastoccur
echo "%lastoccur%"

In echo command are (possible, supposed) cmd -poisonous characters escaped using double quotes .

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