用Perl解析Symphony聊天
我们在工作时从MindAlign改为Symphony聊天。Symphony似乎类似于ZenDesk聊天软件。我们使用Symphony聊天为人们分配门票 当我从Symphony终端剪切和粘贴时,结果是这样的(它没有换行符-它只是一个大的连续文本行): 所以我写这个脚本是为了更好地形成它——它工作得很好用Perl解析Symphony聊天,perl,Perl,我们在工作时从MindAlign改为Symphony聊天。Symphony似乎类似于ZenDesk聊天软件。我们使用Symphony聊天为人们分配门票 当我从Symphony终端剪切和粘贴时,结果是这样的(它没有换行符-它只是一个大的连续文本行): 所以我写这个脚本是为了更好地形成它——它工作得很好 #!/usr/bin/perl use warnings ; use strict ; my $filename = shift @ARGV ; open(my $fh, '<', $fil
#!/usr/bin/perl
use warnings ;
use strict ;
my $filename = shift @ARGV ;
open(my $fh, '<', $filename) or die "Could not open file
$filename $!";
while (my $line = <$fh>) {
chomp $line ;
my @words = split /(\d{2}(?:st|nd|rd|th) \w{3} \d{4})/a, $line;
foreach my $word(@words) {
if($word =~ /\d{2}(?:st|nd|rd|th) \w{3} \d{4}/a) {
chomp $word ;
print $word ;
}
else { print "$word\n"; }
}
}
这就是问题所在-这是我需要的输出:
06/16/2020 WJ: RE: BART failed STP - JIMBCI - INC1019814678
06/16/2020 II : RE: Loans are experiencing issues sending RUNZ - INC1019815218
06/16/2020 NW : RE: FW: Missing pool factor PnL - INC1019816030
06/16/2020 JK : RE: missing sales credits - INC1019816338
06/16/2020 KB : RE: Bookbuilder not responding - INC1019816567
我试过regex的和分裂的行,但它只是一个大混乱。WJ NW JK KB II这些是首字母-它们是常量。有时在冒号(:)后面有空格,有时没有。然而,我所需要的是日期和数据,从票号INC00000000的首字母开始,到最后一位结束 最简单的方法是在打印前“固定”行 所以加上
use Date::Parse;
sub fix_line {
my $output = shift;
if ($output =~ s/^(.*? (am|pm)).*?\s([A-Z]{2} ?:)/$3/) {
my @date = localtime(str2time($1));
$output = sprintf("%02d/%02d/%02d %s", @date[4]+1,@date[3],@date[5]-100,$output) ;
}
return $output."\n";
}
在字符串的底部,将内部fer循环更改为:
my $nl = '';
foreach my $word(@words) {
if($word =~ /\d{2}(?:st|nd|rd|th) \w{3} \d{4}/a) {
chomp $word ;
$nl.= $word ;
}
else {
print fix_line("$nl$word");
$nl=''
}
}
考虑到你有明确的定位点,一种拉出你需要的东西的方法是正则表达式
my @parts = $string =~ /
([0-9]+) # numbers for day
(?:[^0-9]+)?\s+ # for st|nd|rd|th (optional!), space. not captured
(\w+)\s+ # month
([0-9]+)\s+ # year
.+? # the rest, but only up to the initials
( (?:WJ|NW|JK|KB|II): .+? INC[0-9]+ )
/x;
这里的一些模式可以加强或削弱(例如,我们可以使用[A-Z]+:
,而不是预期的首字母的交替,允许其他模式和更多字母)
然后将时间转换为所需的时间戳。这是一个很好的工具。总共
use warnings;
use strict;
use feature 'say';
use Time::Piece;
my $string = q(16th Jun 2020 7:57:18 am Tom Lewin: WJ: RE: BART failed STP - JIMBCI - INC101981467816th Jun 2020 8:20:38 am Nathan Winslow: II : RE: Loans are experiencing issues sending RUNZ - INC101981521816th Jun 2020 8:57:58 am Nathan Winslow: NW : RE: FW: Missing pool factor PnL [Restricted - Internal] - INC101981603016th Jun 2020 9:13:49 am Nathan Winslow: JK : RE: missing sales credits - INC101981633816th Jun 2020 9:24:26 am Nathan Winslow: KB : RE: Bookbuilder not responding - INC1019816567);
my @parts = $string =~ /
([0-9]+) (?:[^0-9]+)?\s+ (\w+)\s+ (\w+)\s+ .+?
( (?:WJ|NW|JK|KB|II): .+? INC[0-9]+ )
/x;
#say for @parts; say '---';
my $dt = Time::Piece->strptime("@parts[0..2]", "%d %b %Y");
say $dt->mdy('/'), ' ', $parts[3];
最后一点也许是最好的做法
my $date = Time::Piece
-> strptime( join(' ', splice @parts, 0, 3), "%d %b %Y")
-> mdy('/');
say "$date @parts";
现在我们不必计算要打印的元素的确切数量
在这种情况下,@parts
最终只有一个元素,但需求确实发生了变化。此外,如果一些元素实际上是为了其他目的而单独存在的(添加捕获括号集),那么@parts
将有更多的元素
这些打印出所需的内容 上面写着“WJ NW JK KB II…它们是常数”——你是说这是唯一可能的两个字母序列吗?或者它可以是任意两个帽子?是的,代表被分配一张票的人-WJ=我,沃尔特·约翰逊
use warnings;
use strict;
use feature 'say';
use Time::Piece;
my $string = q(16th Jun 2020 7:57:18 am Tom Lewin: WJ: RE: BART failed STP - JIMBCI - INC101981467816th Jun 2020 8:20:38 am Nathan Winslow: II : RE: Loans are experiencing issues sending RUNZ - INC101981521816th Jun 2020 8:57:58 am Nathan Winslow: NW : RE: FW: Missing pool factor PnL [Restricted - Internal] - INC101981603016th Jun 2020 9:13:49 am Nathan Winslow: JK : RE: missing sales credits - INC101981633816th Jun 2020 9:24:26 am Nathan Winslow: KB : RE: Bookbuilder not responding - INC1019816567);
my @parts = $string =~ /
([0-9]+) (?:[^0-9]+)?\s+ (\w+)\s+ (\w+)\s+ .+?
( (?:WJ|NW|JK|KB|II): .+? INC[0-9]+ )
/x;
#say for @parts; say '---';
my $dt = Time::Piece->strptime("@parts[0..2]", "%d %b %Y");
say $dt->mdy('/'), ' ', $parts[3];
my $date = Time::Piece
-> strptime( join(' ', splice @parts, 0, 3), "%d %b %Y")
-> mdy('/');
say "$date @parts";