批量检查最后打印的行是否包含单词 [英] Batch check if last printed line contains a word

查看:99
本文介绍了批量检查最后打印的行是否包含单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个批处理文件a.bat和b.bat。 a.bat调用b.bat,而b.bat在屏幕上打印一个句子。如何检查该句子以查看它是否包含单词以及是否包含set作为变量。例如,

 句子:您好,您今天好吗? 
如果%Sentance包含%Hello set var = Hello
如果%Sentance包含%Hi set var = hi



屏幕上可能有多个Sentance,所以我想查看最近显示的情绪。



这就是我的意思。

 用于/ f delims ^ = ^ eol ^ =('b.bat')中的%% i确实设置了最后一行= %% i 
set var =
echo%lastline%| findstr / i \< hi\>> nul&设置 var = hi
如果错误级别1(GOTO NEXT0)否则(GOTO FOUND)
:NEXT0
echo%lastline%| findstr / i \< hello\> > nul&&设置 var = hello
如果错误级别为0(GOTO NEXT1)否则(GOTO FOUND)
:NEXT1
echo%lastline%| findstr / i \< hola\> > nul&&设置 var = hola
如果错误级别0(GOTO NEXT2)否则(GOTO FOUND)
:找到
echo%var%
暂停

如果最后一行类似于这是一条消息,你好,该代码将不起作用

解决方案

回显字符串并搜索关键字:

 设置句子=你好,你好吗? 
echo%sentence%| findstr / i \< hello\>> nul&设置 var = Hello
echo%sentence%| findstr / i \< hi\>> nul&设置 var = Hi

\< \> 的意思是单词边界,这样可以避免误报(C 其他,C 您好,...)



> nul 将找到的字符串重定向到必杀技,以保持屏幕清晰。 / p>

&& 仅执行 set 命令,如果上一个命令( findstr )成功。



编辑



根据您的最新评论,我了解: a.bat 调用 b.bat b.bat 写了几行,而 a.bat 希望得到其中的最后一行(希望,我得到了



来获取 b.bat 的最后一行,请使用:

 用于/ f delims ^ = ^ eol ^ =('b.bat')中的%% i确实设置了最后一行= %% i 
回声你说的:%lastline%
set var =
echo%lastline%| findstr / i \< hello\>> nul&设置 var = Hello
echo%lastline%| findstr / i \< hi\>> nul&设置 var = Hi
echo%lastline%| findstr / i \< hola\>> nul&设置 var = Hola
echo /%var%

但是有一个小问题: for 捕获 b.bat 的输出,而不是将其显示在屏幕上。 (特别是 set / p 的提示-因此您不知道输入的时间或内容)。要解决此问题,请强制 b.bat 写入屏幕(&con; con 直接写入屏幕)。所以基本上, b.bat 应该看起来像这样:

  @echo off 
echo此行被a.bat
> con捕获。echo此行直接进入屏幕
> con set / p input = give me input:
回显有关%input%的信息。

注意:我使用了 eol 技巧来自aschipfl的答案,因为(尽管看起来很丑),即使对于 ; 都有效, 标准方式( delims = eol = )。当然,这仍然不是万无一失的(例如& 仍然会造成问题)


I have 2 batch files a.bat and b.bat. a.bat calls b.bat and b.bat prints a sentence to the screen. How can I check that sentence to see if it contains a word and if it contains a set it as a variable. For example

Sentance: Hello, how are you today?
If %Sentance contains% Hello set var=Hello
If %Sentance contains% Hi set var=hi

There may be more than one Sentance on the screen so I want to check the most recently displayed sentance.

Here's what I have.

for /f delims^=^ eol^= %%i in ('b.bat') do set lastline=%%i
set "var="
echo %lastline%|findstr /i "\<hi\>">nul && set "var=hi"
IF ERRORLEVEL 1 (GOTO NEXT0) ELSE (GOTO FOUND)
:NEXT0
echo %lastline%|findstr /i "\<hello\>">nul && set "var=hello"
IF ERRORLEVEL 0 (GOTO NEXT1) ELSE (GOTO FOUND)
:NEXT1
echo %lastline%|findstr /i "\<hola\>">nul && set "var=hola"
IF ERRORLEVEL 0 (GOTO NEXT2) ELSE (GOTO FOUND)
:found
echo %var%
pause

The code doesn't work if the last line is something like "this is a message hello"

解决方案

echo the string and search for the keyword:

set sentence=Hello, how are you?
echo %sentence%|findstr /i "\<hello\>">nul && set "var=Hello"
echo %sentence%|findstr /i "\<hi\>">nul && set "var=Hi"

\< and \> means "Word boundaries", this avoids false positives (Chinese, Chello,...)

>nul redirects the found string to nirvana to keep the screen clear.

&& executes the set command only, if previous command (findstr) was successful.

Edit

Based on your last comment, I understand: a.bat calls b.bat. b.bat writes several lines, and a.bat wants to get the last of them (I hope, I got that right).

to get the last line of b.bat, use:

for /f delims^=^ eol^= %%i in ('b.bat') do set lastline=%%i
echo you said: %lastline%
set "var="
echo %lastline%|findstr /i "\<hello\>">nul && set "var=Hello"
echo %lastline%|findstr /i "\<hi\>">nul && set "var=Hi"
echo %lastline%|findstr /i "\<hola\>">nul && set "var=Hola"
echo/%var%

But there is a little problem: for captures the output of b.bat instead of showing it to the screen. (Especially the prompt of set /p - so you don't know, when or what to input). To work around that, force b.bat to write to screen (>con writes directly to screen). So basically, b.bat should look like this:

@echo off
echo this line gets captured by a.bat
>con echo this line goes directly to screen
>con set /p "input=give me input: "
echo something about %input%.

Note: I used the eol-trick from aschipfl's answer, because (although it looks ugly) this works even for both " and ;, which are problematic with the "standard way" ("delims= eol="). Of course this still isn't foolproof (for example & still makes problems)

这篇关于批量检查最后打印的行是否包含单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆