正则表达式出错的地方

时间:2013-03-08 06:29:53

标签: regex perl

我有一个查询导致以下数据

查询:

db2 "select RTRIM(substr(A.TBSP_NAME,1,30)),A.TBSP_FREE_PAGES as FREE,B.CONTAINER_NAME as CON_PATH from SYSIBMADM.TBSP_UTILIZATION A ,SYSIBMADM.CONTAINER_UTILIZATION B where A.TBSP_ID=B.TBSP_ID and A.TBSP_AUTO_RESIZE_ENABLED=0 with UR"

结果:

1                              FREE                 CON_PATH                                                                                                                                                                                             
 ------------------------------ -------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 USERSPACE1                                    14736 /adrst/bdts/userspc_container                                                                                                                                                                        
 USERSPACE1                                    14736 /adrst/bdts/userspc_container1                                                                                                                                                                       
 MASTER                                         3472 /adrst/bdts/master_container                                                                                                                                                                         
 TRANS_DATA                                     1200 /adrst/bdts/trans_data_container                                                                                                                                                                     
 MASTER_INDEX                                   1840 /adrst/bdts/master_index_container                                                                                                                                                                   
 TRANSACTION_INDEX                               960 /adrst/bdts/transaction_index_container                                                                                                                                                              
 TEMP_SYS                                       2192 /adrst/bdts/temp_sys_container                                                                                                                                                                       
 AUDIT_DATA                                     3360 /adrst/bdts/audit_data_container                                                                                                                                                                     
 TEMP_USR                                       2672 /adrst/bdts/temp_usr_container                                                                                                                                                                       
 TSASNCA                                        2840 /home/db2inst1/db2inst1/NODE0000/SQL00002/TSASNCA                                                                                                                                                    
 TSASNUOW                                       2880 /home/db2inst1/db2inst1/NODE0000/SQL00002/TSASNUOW                                                                                                                                                   
 TSASNAA                                        3712 /home/db2inst1/db2inst1/NODE0000/SQL00002/TSASNAA                                                                                                                                                    
 TSCDADDRESSMASTER                              2048 /home/db2inst1/db2inst1/NODE0000/SQL00002/CDADDRESSMASTER                                                                                                                                            

   13 record(s) selected.

现在我正在编写一个脚本,它接受第2列并进行比较,如果< 1000然后我们在印刷品中提到col1和col2

所以我写的脚本是

#!/usr/bin/perl
use strict;
use warnings;


`db2 "connect to awdrt"`;
my @tbsp= grep /([a-zA-Z_]*)\s*([0-9]*)\s*([a-zA-Z_]*)/,`db2 "select RTRIM(substr(A.TBSP_NAME,1,30)),A.TBSP_FREE_PAGES as FREE,B.CONTAINER_NAME as CON_PATH from SYSIBMADM.TBSP_UTILIZATION A ,SYSIBMADM.CONTAINER_UTILIZATION B where A.TBSP_ID=B.TBSP_ID and A.TBSP_AUTO_RESIZE_ENABLED=0 with UR"`;

print "@tbsp";

我已经给出了打印测试我成功地消除了'-----'第1行和最后一行......所以我可以拆分并初始化为3个变量并在foreach中进行计算循环,但在某处出错........帮助表示赞赏

3 个答案:

答案 0 :(得分:0)

# loop over the query output
for (`db2 "select ...`) {
  if (my ($c0, $c1, $c2) = /(\w+)\s*(\d+)\s*([\w\/]+)/) {
    print "$c0,$c1,$c2\n";
  }   
}   

答案 1 :(得分:0)

您可以包含过滤具有FREE>的行。 1000个单个grep,如下:

#!/usr/bin/perl
use strict;
use warnings;

my @lines = <DATA>;

my @filtered = grep {/(\S+) # non-space characters
              \s+ # one or more spaces
              (\d+) # numeric characters
              \s+ # one or more spaces
              (\S+) # non-space characters
             /x,
               $2 > 1000 # filter by FREE > 1000
             } @lines;

换句话说,首先使用正则表达式过滤数据行,然后仅返回具有FREE&gt;的行。 1000。

答案 2 :(得分:0)

我得到了一个我问的问题的解决方案.......

#!/usr/bin/perl
use strict;
use warnings;


my $good = "\[\033[32mOK\033[0m\]";
my $bad = "\[\033[31m!!\033[0m\]";


`db2 "connect to awdrt"`;
my @tbsp= grep /\//,`db2 "select RTRIM(substr(A.TBSP_NAME,1,30)),A.TBSP_FREE_PAGES as FREE,B.CONTAINER_NAME as CON_PATH from SYSIBMADM.TBSP_UTILIZATION A ,SYSIBMADM.CONTAINER_UTILIZATION B where A.TBSP_ID=B.TBSP_ID and A.TBSP_AUTO_RESIZE_ENABLED=0 with UR"`;

foreach (@tbsp){
chomp;
s/^\s*//;
s/\s*$//;
my ($col1,$col2,$col3)=split /\s+/,$_,3;
if ($col2 <= 2000){
print "$bad $col1 has $col2 pages with container $col3\n";
#print "alter tablespace $col1 extend (file \'$col3\' 1000)\n";
}
}

它的工作正常.............. 全部谢谢