windows 在命令行输出中搜索字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12453733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:52:54  来源:igfitidea点击:

Search for a string in command line output

windowsbatch-filecommandprompt

提问by Ramesh

I want to search for the string "virtual" in "system model" attribute of 'sysinfo' command. The command should be successful if 'virtual' is found in the 'system model: -------------------------'i.e. output of the systeminfo. It should not search for 'virtual' in whole output of systeminfo command but should do in system model attribute only. For example the command

我想在“ sysinfo”命令的“ system model”属性中搜索字符串“ virtual” 。如果在“系统模型:-------------------------”即 systeminfo 的输出中找到“virtual”,则该命令应该会成功。它不应该在 systeminfo 命令的整个输出中搜索“virtual”,而应该只在系统模型属性中搜索。例如命令

systeminfo | findstr /i "system model" 

I will get something like

我会得到类似的东西

System Model:              HP Compaq dc7800p Small Form Factor

in the above line of the output i want to search for string virtual, and want to manipulate using errorlevel. So please help me to do this.

在上面的输出行中,我想搜索字符串 virtual,并想使用 errorlevel 进行操作。所以请帮助我做到这一点。

Following is the one I tried which was not correct. Or help me if i can use regular expressions

以下是我尝试过的不正确的一种。或者帮助我,如果我可以使用正则表达式

systeminfo | findstr /i /R  "system model: virtual machine" > nul
if %errorlevel% == 0 (
   echo virtual machine
) ELSE (
   echo physical machine
)

Thanks in advance

提前致谢

回答by THelper

Try this:

尝试这个:

systeminfo | findstr /I /B /C:"system model" | findstr /I "virtual"
if %errorlevel% == 0 (
    echo virtual machine
) else (
    echo real machine
)

I've tested in on a real and virtual system and it works fine on WinXp and Win7. Note that the system modelstring is only used in English Windows versions. Windows versions in other languages will use a different names.

我已经在真实和虚拟系统上进行了测试,它在 WinXp 和 Win7 上运行良好。请注意,该system model字符串仅用于英文 Windows 版本。其他语言的 Windows 版本将使用不同的名称。