perl实现收集文本并导入excel

 perl实现收集文本并导入excel

文本文件格式如:

"Begin--------------------------------------------"
"姓名:xxx"
        Physical Address. . . . . . . . . : 00-00-7D-CB-1C-39        IP Address. . . . . . . . . . . . : 172.0.1.168"计算机name:GZZW-fldajdf"
"End--------------------------------------------"
"Begin--------------------------------------------"
"姓名:yyy"
   Physical Address. . . . . . . . . : 00-00-7D-C0-B3-92   IP Address. . . . . . . . . . . . : 172.0.3.194"计算机name:GZxx-dfa"
"End--------------------------------------------"

 

 

要将其中“姓名”、“ip"、”mac—addr“、”computer name“筛选并导入到excel中。

 

以下是perl脚本实现:

 

 

#! perl -w
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::warn = 3;

my $excel = Win32::OLE->GetActiveObject('excel.Application') ||
Win32::OLE->new('Excel.Application','Quit');
my $book = $excel->Workbooks->Add;
my $sheet = $book->worksheets(1);

my $file = shift;
die "Usage:$0 getaddress file./n/n" if (!$file);

open(FILE,"<$file") or die "open file false:$!";

#my $strtest= "llworldtestyulintestlintest";
#my @str0=~m/(test)/g;
#print $str0[0];

my $strtemp = "";
my $str1;
my $num =  0;
my $lie = 1;
my $iptem;
my $mactem;
while(<FILE>){
             #$strtemp = <FILE>;
  #print "$strtemp";

 
          $str1.=$_;
            if(/^.*End-/){
             $num++; 
     $str1=~s//n//g;
     $str1=~s/-------------------------------------------//*/g;
     
     
     while($str1=~s/(?<=/:)/S{1,30}(?=/")//){
                         print "$& ";
                         $sheet->Cells($num,$lie)->{Value}=$&;
                         $lie++;
                        }
                        $lie=1;
     while($str1=~s//d+/./d+/./d+/./d+//){
                        
                        $iptem.=$&;
                        $iptem.=" ";
                        $sheet->Cells($num,3)->{Value}=$iptem;
                       }$iptem="";
     while($str1=~s//w/w-/w/w-/w/w-/w/w-/w/w-/w/w//){
                             $mactem.=$&;
                             $mactem.=" ";
                             $sheet->Cells($num,4)->{Value}=$mactem;
                             }$mactem="";
                             print "/n";
          
     #####print "$str1 /n";
     #print "$`/n";
             $str1="";
     #print "$&/n";
                           }
                 }

$book->SaveAs('E:/linux/perl/protest/test1.xls');
$book->Close;
#print "num=$num /n";

http://svn.perl.org/modules/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值