2012-08-30 151 views
1

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

命令

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

正如我可以在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 

因此,这可以用Java轻松解析通过分裂与\s{2,}正则表达式:

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] 

对于该批次的一面,我帮不了你,因为我对此一无所知:-)

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

+0

如果只有1个空格将列分隔开,则问题可能仍然存在:( – abi1964

+0

是的,但您的示例中并非如此。除此之外,柱子突然被一个空间隔开,会不会很奇怪?我找不到这个命令的输出格式,所以我帮不了你。这应该在实践中工作 – Alex

+0

看到我的编辑,显然有一个开关'/格式'为wmic。 – Alex