Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove empty lines from wmic output?

This is my string to obtain the position of TeamViewer (any version) service executable:

for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname') do set POSITION=%A

The problem is caused by wmic because it includes an empty line at the end of result (on Windows 7 command) and this is the output:

C:\Users\giovanni>for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%
'" get pathname') do set POSITION=%A

 :\Users\giovanni>set POSITION="C:\Program Files\TeamViewer\Version8\TeamViewer_Service.exe"

 :\Users\giovanni>set POSITION=

C:\Users\giovanni>echo %position%
ECHO enabled.

How I can get only the second line of the output with the correct position of the executable? (or skip the latest line, of course).

Thanks all in advance and have a nice day. Giovanni.

This is checktv.bat:

for /f "skip=1 delims=" %%A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"') do set POSITION=%%A
echo %POSITION%
like image 236
Giovanni Avatar asked Mar 28 '13 15:03

Giovanni


People also ask

How do I remove blank lines from output?

Delete blank lines using the grep commandWhen used with the -v option, the grep command helps to remove blank lines. Below is a sample text file, sample. txt, with alternative non-empty and empty lines. To remove or delete all the empty lines in the sample text file, use the grep command as shown.

Which command is used to remove empty lines?

The d command in sed can be used to delete the empty lines in a file.


2 Answers

Like this:

for /f "skip=1 delims=" %A in (
  'wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"'
) do set POSITION=%A

The findstr /r /v "^$" removes empty lines from the output.

like image 60
Ansgar Wiechers Avatar answered Sep 23 '22 13:09

Ansgar Wiechers


wmic blah /value | find "=" >> wherever

output will be

field=value

no extra lines

tokenize from there, delim =

like image 36
David Lang Avatar answered Sep 22 '22 13:09

David Lang