2013-02-22 26 views
0

我的要求如下:讀WMIC命令的輸出到一個單一的陣列,並且處理所述陣列在Perl

我正在讀的WMIC命令的輸出如下所示:

my @pnames = `wmic product where "Name like '%Provider%'" get IdentifyingNumber, Name,Version`; 

print "@pnames"; 

直到這裏的輸出如下:

IdentifyingNumber      Name          Version 

{E094B8EA-87B7-48DE-A0A8-A18AC8BFCDF4} .NET Data Provider for Teradata 14.00.0.0 14.00.0.0 

{1551F9D6-1B14-4AE1-BABA-70A4319C236A} ODBC Driver Provider      14.00.0.0 

Proceesing陣列

foreach $name (@pnames) 
{ 
    chomp($name); 

    #@names = split('need to pass something',$name); 
    # I want to split every input entry into 3 parts- IdentifyingNumber, Name,Version similarly the way they read. 

    $name =~ s/^\s+//; #remove leading spaces 
    $name =~ s/\s+$//; #remove trailing spaces 

    # here logic goes this way, If the version number has matched, serach for name and if name has matched display its identifying number 

    print "Identifying number(say $id) of the packge with name(say $pkg)\n"; 

} 

我在緊急要求。任何其他方法也非常感謝。誰能幫幫我嗎??在此先感謝

+0

嘿謝謝Zaid ...其實我想這樣說...非常感謝你 – ybc 2013-02-22 10:10:27

回答

0

你可以通過匹配您輸入線

foreach my $line (@pnames) { 
    my($id, $name, $version) = ($line =~ m/^\s*({.+?})\s+(.+?)\s+([\d.]+)\s*$/); 
    ... 
} 

這抓住了三個部分(...)並將其分配給變量$id$name$version分裂。

+0

'(。+)\ s +'相當於'(。+)\ s'。 – melpomene 2013-02-22 11:08:17

+0

@melpomene這取決於,如果你想要任何尾隨空白或不。 – 2013-02-22 11:11:26

+0

它不依賴。它是等價的。 – melpomene 2013-02-22 11:12:17