如何在wmic输出中使用分隔符,分隔列?

时间:2012-08-30 07:14:42

标签: java windows windows-7 groovy wmic

我想以某种格式获取Windows Hotfix列表,其输出可以用一些分隔符分隔。到目前为止,我发现了一个wmic命令,它给了我一个所需的输出,但问题是\s分隔符在这里不起作用。有没有办法可以放置一些,或任何其他字符,我以后可以在java程序中使用它来获取单个列?

命令

wmic qfe get caption,csname,description,hotfixid,installedby,installedon

输出

Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
..
.

更新

我正在尝试

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe get caption,csname,description,fixcomments,hotfixid,installdate,installedby,installedon,name,servicepackineffect,status') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p

但它给了我无效的GET表达

C:\Users\Abhishek\Desktop>for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe get caption,csname,description,fixcomments,hotfixid,installdate,installedby,installedon,name,servicepackineffect,status') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p
Invalid GET Expression.

这是什么问题?这可能会解决我的问题。

更新

我甚至尝试了以下命令,但这也没有解决空间问题

命令

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe list') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p

输出

Caption,CSName,Description,FixComments,HotFixID,InstallDate,InstalledBy,InstalledOn,Name,ServicePackInEffect
http://go.microsoft.com/fwlink/?LinkId=161784,Abhishek,Update,KB971033,NT,AUTHOR,,Y\SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2281679,Abhishek,Security,Update,KB2281679,NT,AUTHORITY\SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2284742,Abhishek,Update,KB2284742,NT,AUTHORIT,,SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2286198,Abhishek,Security,Update,KB2286198,NT,AUTHORITY\SYSTEM,3/15/2012,

3 个答案:

答案 0 :(得分:4)

正如我在wmic的结果中看到的那样,列 至少 2个空格分开。

Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012

因此,可以通过使用\s{2,}正则表达式

进行拆分,在Java中轻松解析
String result = "...";
for (String line : result.split("\n")) {
     System.out.println("-> " + line);
     for (String column : line.split("\\s{2,}")) {
         System.out.println("  => [" + column.trim() + "]");
     }
 }

输出:

-> Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
  => [Caption]
  => [CSName]
  => [Description]
  => [HotFixID]
  => [InstalledBy]
  => [InstalledOn]
-> http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
  => [http://go.microsoft.com/fwlink/?LinkId=161784]
  => [Abhishek]
  => [Update]
  => [KB971033]
  => [NT AUTHORITY\SYSTEM]
  => [3/15/2012]
-> http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
  => [http://support.microsoft.com/?kbid=2032276]
  => [Abhishek]
  => [Security Update]
  => [KB2032276]
  => [NT AUTHORITY\SYSTEM]
  => [3/15/2012]

对于批处理方面,我无法帮助你,因为我对此一无所知: - )

修改 显然,/format:csv有一个wmic开关,它应该生成一个CSV,你也可以用Java轻松解析。我无法让它在我的机器上工作,但值得注意。

答案 1 :(得分:0)

答案 2 :(得分:0)

我最近为这种事情做了一些事情,并设法使其与以下语法一起使用。下面的代码段。

FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ('WMIC /NODE:"%PCNAME%" PRODUCT GET 
IDENTIFYINGNUMBER^,INSTALLSOURCE^,NAME^,PACKAGECACHE^,PACKAGENAME^,VENDOR^,VERSION 
/FORMAT:CSV') DO (
 SET ELE1=%%a
 SET ELE2=%%b
 SET ELE3=%%c
 SET ELE4=%%d
 SET ELE5=%%e
 SET ELE6=%%f
 SET ELE7=%%g
 SET ELE8=%%h
 CALL :CheckIt
)