如何使用cmd命令在txt中找到包含字符串的最后一行

How can I find last row that contains string in txt with cmd command

我想在 .txt 文件中找到两个不同的字符串。我需要两个脚本,第一个脚本查找包含这些字符串的最后一行,第二个脚本查找单独包含这些字符串的最后一行。我试着写点东西,但我放弃了这个项目。

这是我迄今为止尝试过的代码:

@echo off
for /f %%i in ('find /v /c "" ^< deneme.txt') do set /a lines=%%i
echo %lines%
set /a startLine=%lines% - 1
more /e +%startLine% deneme.txt > temp.txt
find "ali" temp.txt|find "veli" 
del temp.txt

感谢您的帮助。

@echo off
set "string1=ali"
set "string2=veli"
set "file=deneme.txt"

for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i /v "\<%string2%\>" ') do set "out1=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string2%\>" %file% ^|findstr /i /v "\<%string1%\>" ') do set "out2=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i "\<%string2%\>" ') do set "out3=%%a"

echo last line with %string1%:   "%out1%"
echo last line with %string2%:   "%out2%"
echo last line with both:        "%out3%"

有关解释,请参阅 for /?findstr /?