2012-08-30 201 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