2016-03-29 70 views
-1

我让下面的脚本,但脚本运行的结果有误差 “在级联(。)或字符串初始化值$ bay_name使用在test.pl管线92”如何让脚本运行?

#!/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 

我想下面的结果 湾Name行是删除了把 所有内容变化的打击格式

{ 
      "{#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

没有使用所有的shell管道和函数,你可以用一点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 

这将产生以下输出加入

my $bay_name; 

for循环内根据您的示例数据

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(/(数目)\ S +:\ S +(\ d +)/){ 打印 “$ bay_name $ 1:$ 2 \ n” 个; } ELSIF(/(全部)\ S +:\ S +(\ S +)/){ 打印 “$ bay_name $ 1:$ 2 \ n” 个; }' – ZXC

+0

你需要使用'ELSIF(/(数*)\ S +:\ S +(\ d +)/){'和'ELSIF(/(All.*)\s+:\s+(\S+) /){'将匹配包含全部和数字的行。但是,我不确定为什么你会这样做,除非你的文件包含你还没有显示的其他行,你也试图捕捉。 –

1

你问的实际问题是固定的。你打开了use strict,这是很好的做法,需要声明变量,但没有声明$bay_name,这是错误消息引用的内容。

然而,其他人指出,从调用Perl的管道是完全不必要的,可以改善各种其它方式的代码。