那么,这取决于你想要对数据做什么。
假设你有解决这个大while (<>) { ... }
,您可以通过只使用分割得到的最简单的解析:
my @fields = split;
下一级将增添几分意义
my ($date, $time, $id, $host, $from, $to, undef, $dest) = split;
(注,如果你想忽略一个结果,你可以指定为undef
)
最后,你可以通过使用正则表达式清理大量的文件裂变。您也可以将上面的拆分与较小的正则表达式组合起来,分别清理每个字段。
my ($datetime, $id, $host, $from, $to, $dest) =
/([\d-]+ [\d:]+) \s+ # date and time together
(\S+) \s+ # message id, just a block of non-whitespace
<(.*?)> \s+ # hostname in angle brackets, .*? is non-greedy slurp
\((.*?)\) \s+ # from email in parens
<(.*?)> \s+ # to email in angle brackets
\S+ \s+ # separated between to-email and dest
(\S+) # last bit, could be improved to (\w)=\[(.*?)\]
/x; # /x lets us break all of this up, so its a bit readable
当然,你可以继续服用这种种愚蠢的,但如果你要开始做这些领域的更具体的分析,我会与最初的拆分之后broken-去out字段解析。例如:
my ($date, $time, ...) = split;
my ($year, $month, $day) = split(/-/, $date);
my ($hour, $min, $sec) = split(/:/, $time);
my ($from_user, $from_host) = ($from =~ /< ([^\@]+) \@ (.*) >/x);
...etc...
这个问题有太多问题。请一次发布一个**特定**问题。 – Mat 2011-04-21 05:22:18
好的。虐待编辑这个。 – MealstroM 2011-04-21 06:22:33