String 文本文件(PERL)中日志输入和输出片段的拆分字符串变量
我的日志文件中有以下条目String 文本文件(PERL)中日志输入和输出片段的拆分字符串变量,string,perl,logging,output,editor,String,Perl,Logging,Output,Editor,我的日志文件中有以下条目 [2016-04-17 10:12:27:682011 GMT] tcp 115.239.248.245:1751 -> 192.168.0.17:8080 52976f9f34d5c286ecf70cac6fba4506 04159c6111bca4f83d7d606a617acc5d6a58328d3a631adf3795f66a5d6265f4d1ec99977a5ae8cb2f3133c9503e5086a5f2ac92be196bb0c9a9f653f9
[2016-04-17 10:12:27:682011 GMT] tcp 115.239.248.245:1751 -> 192.168.0.17:8080 52976f9f34d5c286ecf70cac6fba4506 04159c6111bca4f83d7d606a617acc5d6a58328d3a631adf3795f66a5d6265f4d1ec99977a5ae8cb2f3133c9503e5086a5f2ac92be196bb0c9a9f653f9669495 (312 bytes)
我想写一个脚本,将这一行字符串分割成若干段,以便将其中一些段写入.csv文件中,用于机器学习。到现在为止,我用这个脚本来找到一个特定的模式,如果找到了,写下它要找到的内容,硬编码搜索。这不是我想要的。这是我现在的剧本
#!/usr/bin/perl -w
$path1 = "/home/tsec/testwatch/attackerresult.log";
$attacker = ">>/home/tsec/testwatch/attacker.csv";
#$path2 =
#$path3 =
#$path4 =
#function definition #Pattern for attackerlog only
sub extractor(){
open(LOG, $path1) or die "Cant't open '$path1': $!";
open(FILE, $attacker) or die "Can't open '$attacker': $!";
$target = "tcp";
while(<LOG>){
if(/$target/){
print FILE $target . "\n";
}
}
}
close(LOG);
close(FILE);
#/usr/bin/perl-w
$path1=“/home/tsec/testwatch/attackerresult.log”;
$attacker=“>>/home/tsec/testwatch/attacker.csv”;
#$path2=
#$path3=
#$path4=
#函数定义#仅用于attackerlog的模式
副提取器(){
打开(日志,$path1)或死亡“无法打开$path1:$!”;
打开(文件,$attacker)或死亡“无法打开$attacker:$!”;
$target=“tcp”;
while(){
如果(/$target/){
打印文件$target。“\n”;
}
}
}
关闭(日志);
关闭(文件);
我希望CSV文件中的输出如下:
我可以手动完成csv标题
(标题)协议、源IP地址、源端口、文件大小
(脚本的字符串结果)tcp,127.0.0.180312
以上只是一个例子
有什么想法吗 如果所有行始终具有相同数量的字段,这将起作用
use warnings;
use strict;
open my $wfh, '>', 'out.csv' or die $!;
my $cols = "Protocol, Source IP Address, Source Port, File Size\n";
print $wfh $cols;
while (<DATA>){
if (/
(?:.*?\s){3} # get rid of the time
(.*?) # capture the proto ($1)
\s+ # skip the next whitespace
(.*?):(\d+) # separate IP and port, capture both ($2, $3)
.*?\( # skip everything until an opening parens
(\d+) # capture bytes ($4)
/x
){
print $wfh "$1, $2, $3, $4\n";
}
}
__DATA__
2016-04-17 10:12:27:682011 GMT tcp 115.239.248.245:1751 -> 192.168.0.17:8080 52976f9f34d5c286ecf70cac6fba4506 04159c6111bca4f83d7d606a617acc5d6a58328d3a631adf3795f66a5d6265f4d1ec99977a5ae8cb2f3133c9503e5086a5f2ac92be196bb0c9a9f653f9669495 (312 bytes)
所需的输出似乎与您显示的输入无关
Protocol, Source IP Address, Source Port, File Size
tcp, 115.239.248.245, 1751, 312