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";