在shell脚本中解析wmic的输出 [英] Parsing the output of wmic in shell script

查看:213
本文介绍了在shell脚本中解析wmic的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析WMIC的输出,然后获取PID.

I am trying to parse the output of WMIC and then to get the PID.

我的脚本如下:

@echo off

setLocal enableExtensions enableDelayedExpansion

FOR /F "tokens=1* delims=" %%A IN ('"wmic process where(name="java.exe") get ProcessID,commandline| FINDSTR /v "CommandLine" | FINDSTR "TestServer""') DO (
     set "line=%%A"
     @REM echo "%%A"
     for /F "tokens=* delims= " %%C in ("%%A") do (
         echo "%%C"
         echo "%%D"
  )
)

输出如下:

"java  com.test.TestServer                                       7560       "
"%D"
"java  com.test.TestServer                                       7380       "
"%D"

我的目标是获取进程ID.

My target here is to grab the Process ID.

我已经尝试过将空间作为FOR循环中的delims.仍然没有运气.

I have tried with space as the delims in the FOR loop. Yet no luck.

所以我的问题是如何格式化WMIC的列并获取列?

So my question is how to format the columns of the WMIC and to get the columns?

推荐答案

WMIC使用SQL语法的子集.您可以通过使用LIKE运算符(以%作为通配符)在功能上将FINDSTR测试放在WMIC WHERE子句中.由于它位于批处理脚本中,因此%需要加倍.

WMIC uses a subset of SQL syntax. You can functionally put the FINDSTR tests in the WMIC WHERE clause by using the LIKE operator with % as the wildcard. Since it is in a batch script, the % needs to be doubled.

解析出ProcessID容易得多,因为WMIC输出中不再需要CommandLine值.我添加了SessionID,以便在末尾放置一个未使用的值,以便更轻松地解析该值,而不必担心FOR/F在将WMIC Unicode输出转换为ANSI时以某种方式引入的不必要的回车符.

It is much easier to parse out the ProcessID since the CommandLine value is no longer needed in the WMIC output. I added the SessionID to put an unused value at the end to make it easier to parse the value without worrying about the unwanted carriage return character that FOR /F somehow introduces when it converts the WMIC unicode output into ANSI.

@echo off
setlocal
set "pid="
for /f "skip=1" %%A in (
  'wmic process where "name='java.exe' and commandline like '%%TestServer%%' and not commandline like '%%CommandLine%%'" get processid^, sessionid'
) do if not defined pid set "pid=%%A"


编辑评论中的问题


EDIT in response to question in comment

WMIC Unicode的FOR/F转换还引入了看似为空但实际上包含单个回车符的不需要的行.在上面的代码中,我假设您期望一行,因此我使用IF DEFINED有效地忽略了多余的尾随空白"行.

The FOR /F conversion of WMIC unicode also introduces unwanted lines that look to be empty but actually contain a single carriage return. In my code above I assume you expect a single line, so I use an IF DEFINED to effectively ignore the unwanted trailing "blank" line.

如果期望多个值,则需要另一种方法.额外的FOR/F循环将删除不需要的回车符,因此这些行实际上变为空行,因此将被忽略.根据您的评论,您似乎不再需要忽略包含"CommandLine"的命令行.

Another approach is needed if you expect multiple values. An extra FOR /F loop will remove the unwanted carriage returns, so the lines truly become empty and are therefore ignored. Based on your comment, it looks like you no longer need to ignore command lines that contain "CommandLine".

我不知道您需要使用这些值做什么,因此下面的代码简单地回显每个值.我使用SKIP = 1来代替通过FINDSTR传递结果以避免标题行.

I don't know what you need to do with the values, so the code below simply echoes each value. I use SKIP=1 in place of piping the result through FINDSTR to avoid the header line.

@echo off
for /f "skip=1" %%A in (
  'wmic process where "name='java.exe' and commandline like '%%TestServer%%'" get processid'
) do for /f %%B in ("%%A") do echo %%B

这篇关于在shell脚本中解析wmic的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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