我是perl的新手。 我有一个文件,我需要使用perl脚本解析。
文件格式如下:
05\/26\/2013 06:09:47 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - uid=radash@abc.com\,ou=People\,o=zeb.com - 06:09:47 - http - uizweb_zam - - 2uid=bolched@abc.com
05\/26\/2013 06:09:48 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - uid=radash2s@abc.com\,ou=People\,o=zeb.com - 06:09:48 - http - uizweb_zam - - 2uid=bolchedssd@abc.com
05\/26\/2013 06:09:49 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.43 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - uid=sjhsjdh@abc.com\,ou=People\,o=zeb.com - 06:09:49 - http - uizweb_zam - - 2uid=kjsdsdjhjsh@abc.com
从文件我想得到这样的东西:
05/26/2013 06:09:49 and uid=radash@abc.com,ou=People,o=zeb.com
来自每一行。
我尝试过split(),但我无法正确使用它。
请帮忙
答案 0 :(得分:1)
带分割的简单脚本
my $IP_FILE=$ARGV[0];
open(IP_FILE,$IP_FILE) || die "Unable to open file...";
while(<IP_FILE>)
{
chomp;
my @vals = split("-");
$vals[0] =~ s/\\//g;
$vals[7] =~ s/\\//g;
printf("%s %s\n",$vals[0],$vals[7]);
}
close(IP_FILE);
答案 1 :(得分:1)
perl -ape '$_ = "$F[0] $F[1] and $F[14]\n"; s|\\||g;' file
答案 2 :(得分:1)
#open file
open(FILE, "FILENAME.txt") || die "Can't open file";
# read file into an array
@data = <FILE>;
close(FILE);
foreach $line(@data) {
@output = split / /, $line;
$output[0] =~ s/\\//g;
print "$output[0] $output[1] and $output[16]\n";
}
或者您可以将其推迟使用。
#open file
open(FILE, "FILENAME.txt") || die "Can't open file";
# read file into an array
@data = <FILE>;
close(FILE);
foreach $line(@data) {
@output = split / /, $line;
$output[0] =~ s/\\//g;
push @result, "$output[0] $output[1] and $output[16]\n";
}
print @result;
代码不是很干净,因为如果文件的数据移动到文件中的其他位置,它会产生不正确的结果,但是这样可以解决分割的工作原理。
关于你的链接问题。这是一本很好的初学者电子书。
http://bookboon.com/en/perl-for-beginners-ebook
答案 3 :(得分:0)
perl -lane '$a="@F[0,1]";$a=~s/[\\\/]//g;$F[14]=~s/\\//g;print "$a and $F[14]"' your_file
答案 4 :(得分:0)
如果您知道分隔符-
,则可以使用Text::ParseWords
对其进行分析。它是Perl 5中的核心模块。我提供正则表达式\s*-\s*
来删除破折号周围的空白。
use strict;
use warnings;
use Text::ParseWords;
use feature 'say';
while (<DATA>) {
chomp;
my @data = quotewords('\s*-\s*', 0, $_);
say join " ", @data[0, 6];
}
__DATA__
05\/26\/2013 06:09:47 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - uid=radash@abc.com\,ou=People\,o=zeb.com - 06:09:47 - http - uizweb_zam - - 2uid=bolched@abc.com
05\/26\/2013 06:09:48 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - uid=radash2s@abc.com\,ou=People\,o=zeb.com - 06:09:48 - http - uizweb_zam - - 2uid=bolchedssd@abc.com
05\/26\/2013 06:09:49 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.43 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - uid=sjhsjdh@abc.com\,ou=People\,o=zeb.com - 06:09:49 - http - uizweb_zam - - 2uid=kjsdsdjhjsh@abc.com
<强>输出:强>
05/26/2013 06:09:47 -0700 uid=radash@abc.com,ou=People,o=zeb.com
05/26/2013 06:09:48 -0700 uid=radash2s@abc.com,ou=People,o=zeb.com
05/26/2013 06:09:49 -0700 uid=sjhsjdh@abc.com,ou=People,o=zeb.com