Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for a string in command line output

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

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.

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

like image 684
Ramesh Avatar asked Sep 17 '12 05:09

Ramesh


People also ask

How do I search for something in the command prompt?

By default, this command is set to ctrl+shift+f . // Press ctrl+shift+f to open the search box { "command": "find", "keys": "ctrl+shift+f" }, For example, you can change "ctrl+shift+f" to "ctrl+f", so when typing ctrl+f , the search dialog will open.

What does this command string do find?

Specifies the text string to find. Specifies a file or files to search. If a pathname is not specified, FIND searches the text typed at the prompt or piped from another command.


1 Answers

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 model string is only used in English Windows versions. Windows versions in other languages will use a different names.

like image 142
THelper Avatar answered Oct 10 '22 06:10

THelper