2016-03-29 32 views
-1

以下のスクリプトを作成しますが、スクリプトの実行結果にエラーがあります "test.pl行92で連結されていない値$ bay_nameを使用しています。スクリプトの実行方法を教えてください。

#!/usr/bin/env perl 

use strict; 
use warnings; 
use JSON; 
use Getopt::Long; 
use POSIX qw/strftime/; 


## perl time variable 
my $dt = strftime('%Y%m%d%H%M%S',localtime); 


## Variable reset 
my ($key, $value, $bay_name, $dot) = ''; 
$dot = ' '; 

for (`cat test | awk 'NF' | awk /:/ | sed -e 's/ \+/ /g' -e 's/ : /:/g' | egrep "All|Bay|Number"`) 
    { 
     ($key, $value) = m/(.*?):(.*?)$/g 
      or next; 

     if ($key eq 'BayName') { 
      $bay_name = $value; 
     } 
     else { 
      $key = $bay_name . $dot . $key; 
     } 

     if ($key eq 'BayName') { 
      undef $key; 
     } 
     else { 
     print "\t,\n"; 
     print "\t{\n"; 
     print "\t\t\"{#HWNAMES}\":\"$key\",\n"; 
     print "\t\t\"{#HWSTATUS}\":\"$value\"\n"; 
     print "\t}\n"; 
    } 
    } 

私はそれが

テストファイルを修正したいが ベイ名前行が すべてのコンテンツの変更ブローフォーマットを入れて中に削除され、私は結果を下回るたい

System Bay 

    Bay Name        : SB-1 
    Number of Standby Power Supplies  : 4 
    Number of Drive Enclosures   : 0 
    Number of Enclosure Slots   : 2 
    Number of MIBE Enclosures   : 2 

    Summary Status of Contained Modules 
    All Standby Power Supplies   : Normal 
    All Enclosures      : Normal 
    All Link Control Cards    : Normal 
    All Power Supplies     : Normal 
    All Enclosure Slots    : Normal 
    All Power Supplies     : Normal 
    All Fans       : Normal 
    All Management Modules    : Normal 
    All IO Module Carriers    : Normal 
    All Directors      : Normal 
    All MIBE Enclosures    : Normal 
    All Power Supplies     : Normal 

Drive Bays 

    Bay Name        : DB-1A 
    Number of Standby Power Supplies  : 8 
    Number of Drive Enclosures   : 16 

    Summary Status of Contained Modules 
    All Enclosures      : Normal 
    All Link Control Cards    : Normal 
    All Power Supplies     : Normal 
    All Standby Power Supplies   : Normal 

コンテンツの下にあり

{ 
      "{#HWNAMES}":"SB-1 All Standby Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Enclosures", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Link Control Cards", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Enclosure Slots", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Fans", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Management Modules", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All IO Module Carriers", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Directors", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All MIBE Enclosures", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    } 

以下は私のスクリプトです

for (`/opt/emc/SYMCLI/bin/symcfg -sid $sid list -env_data`) { 

     if (/^\s+Bay Name\s+:\s+(\S+)$/){ 
       $bay_name = $1; 

     } elsif (/(Number.*)\s+:\s+(\d+)/){ 
       print "\t{\n"; 
       print "\t\t\"{#HWNAMEC}\":\"$bay_name $1\",\n"; 
       print "\t\t\"{#HWCOUNT}\":\"$2\"\n"; 
       print "\t},\n"; 


     } elsif (/(All.*)\s+:\s+(\S+)/) { 
       print "\t{\n"; 
       print "\t\t\"{#HWNAMES}\":\"$bay_name $1\",\n"; 
       print "\t\t\"{#HWSTATUS}\":\"$2\"\n"; 
       print "\t},\n"; 
     } 
} 
+0

おそらく 'tail -n +5 |あなたのファイルの残りの部分を実際に処理するためにhead -n-1部分... – xxfelixxx

+0

あなたのコードmetnions "BayName"しかし、あなたの猫の出力は "Bay Name"です。あなたの変数を確認するためにデータダンパーを使用する必要があります –

答えて

1

シェルパイプと関数をすべて使用せずに、少しでもperlでこれを実現できます。

use strict; 
use warnings; 

my $bay_name; 
while (<DATA>) { 
    chomp(); 
    if (/^\s+Bay Name\s+:\s+(\S+)$/){ 
     $bay_name = $1; 
    } elsif (/(Number of Standby Power Supplies)\s+:\s+(\d+)/){ 
     print "$bay_name $1 : $2\n"; 
    } elsif (/(All Standby Power Supplies|All Enclosures)\s+:\s+(\S+)/) { 
     print "$bay_name $1 : $2\n"; 
    } 
} 


__DATA__ 
System Bay 

    Bay Name        : SB-1 
    Number of Standby Power Supplies  : 4 

    Summary Status of Contained Modules 
    All Standby Power Supplies   : Normal 

Drive Bays 

    Bay Name        : DB-1A 
    Number of Standby Power Supplies  : 8 

    Summary Status of Contained Modules 
    All Enclosures      : Normal 

これはあなたのforループ内

my $bay_name; 

を追加することによって、あなたの例のデータ

SB-1 Number of Standby Power Supplies : 4 
SB-1 All Standby Power Supplies : Normal 
DB-1A Number of Standby Power Supplies : 8 
DB-1A All Enclosures : Normal 
+0

ありがとうございました。私は "すべての"コンテンツと "番号"を含む変更elseifをしたい。どのように作ることができますか? – ZXC

+0

'elsif(/(Number)\ s +:\ s +(\ d +)/){ " $ bay_name $ 1:$ 2 \ n "; } elsif(/(すべて)\ s +:\ s +(\ S +)/){ "$ bay_name $ 1:$ 2 \ n";} ' – ZXC

+0

' elsif(/(Number。*)\ s +:\ s +(\ d +)/){'と' elsif(/(All.*)\s+:\s+(\S+) /){'はAllとNumberを含む行と一致します。しかし、あなたのファイルにあなたがまだキャプチャしようとしていない他の行が含まれていない限り、なぜそうしたいのか分かりません。 –

1

あなたが尋ねた実際の質問が固定されているに基づいて、次の出力を生成します。 use strictが有効になっていますが、それは良い習慣であり、変数の宣言が必要ですが、エラーメッセージの参照先である$bay_nameの宣言はありません。

しかし、Perlからパイプラインを呼び出すことはまったく必要ではなく、さまざまな方法でコードを改善することができます。

関連する問題