1

我是 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(),但我无法正确获取它。

请帮忙

4

5 回答 5

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);
于 2013-05-29T05:58:07.477 回答
1
perl -ape '$_ = "$F[0] $F[1]  and $F[14]\n"; s|\\||g;' file
于 2013-05-29T06:13:02.213 回答
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
于 2013-05-29T06:25:03.503 回答
0
perl -lane '$a="@F[0,1]";$a=~s/[\\\/]//g;$F[14]=~s/\\//g;print "$a and $F[14]"' your_file
于 2013-05-29T06:04:32.917 回答
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
于 2013-05-29T08:25:04.727 回答