这是FIX协议Log
2015-11-19 12:11:34,050INFO [Fix message assembler]TWORDERS42_UBS-<1356 OrderSingle (8=FIX.4.2 9=195 35=D 34=1356 49=TWORDERS42 52=20151119-17:11:34.049 56=UBS 57=PROG 97=N 115=GUARDIAN 11=AFMC286;6CE7-7DF5 15=USD 21=1 22=1 38=4481 40=1 48=031162100 54=1 55=AMGN 59=0 60=20151119-17:01:11 92=CSA 10=028 )
2015-11-19 12:11:34,050INFO [SessionManager for CamUSP3: TWORDERS42 UBS]USA4P3_42_CamUSP3_42->4735 OrderSingle (8=FIX.4.2 9=197 35=D 34=4735 49=TWORDERS42 52=20151119-17:11:34 56=USA4P3_42 57=PROG 97=N 115=GUARDIAN 11=AFMC286;6CE7-7DF5 15=USD 21=1 22=1 38=4481 40=1 48=031162100 54=1 55=AMGN 59=0 60=20151119-17:01:11 92=CSA 10=210 )
2015-11-19 12:12:54,468INFO [Fix message assembler]USA4P3_42_CamUSP3_42-<68210 ExecutionReport (8=FIX.4.2 9=285 35=8 34=68210 52=20151119-17:12:54 43=N 49=USA4P3_42 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4481 150=0 11=AFMC286;6CE7-7DF5 31=0.0000 32=0 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=0 38=4481 40=1 37=42348647 17=842696664 6=0.0000000 14=0 60=20151119-17:12:54 75=20151119 10=099 )
2015-11-19 12:12:54,468INFO [SessionManager for CamUSP3: USA4P3_42 CamUSP3_42]TWORDERS42_UBS->9012 ExecutionReport (8=FIX.4.2 9=278 35=8 34=9012 52=20151119-17:12:54 43=N 49=UBS 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4481 150=0 11=AFMC286;6CE7-7DF5 31=0.0000 32=0 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=0 38=4481 40=1 37=42348647 17=842696664 6=0.0000000 14=0 60=20151119-17:12:54 75=20151119 10=181 )
2015-11-19 12:13:58,300INFO [Fix message assembler]USA4P3_42_CamUSP3_42-<68476 ExecutionReport (8=FIX.4.2 9=303 35=8 34=68476 52=20151119-17:13:58 43=N 49=USA4P3_42 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4381 150=1 11=AFMC286;6CE7-7DF5 29=1 31=159.0800 32=100 30=O 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=1 38=4481 40=1 37=42348647 17=842697795 6=159.0800000 14=100 60=20151119-17:13:58 75=20151119 10=243 )
2015-11-19 12:13:58,300INFO [SessionManager for CamUSP3: USA4P3_42 CamUSP3_42]TWORDERS42_UBS->9061 ExecutionReport (8=FIX.4.2 9=296 35=8 34=9061 52=20151119-17:13:58 43=N 49=UBS 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4381 150=1 11=AFMC286;6CE7-7DF5 29=1 31=159.0800 32=100 30=O 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=1 38=4481 40=1 37=42348647 17=842697795 6=159.0800000 14=100 60=20151119-17:13:58 75=20151119 10=068 )
2015-11-19 12:14:16,245INFO [Fix message assembler]USA4P3_42_CamUSP3_42-<68598 ExecutionReport (8=FIX.4.2 9=304 35=8 34=68598 52=20151119-17:14:16 43=N 49=USA4P3_42 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4281 150=1 11=AFMC286;6CE7-7DF5 29=2 31=159.0850 32=100 30=65 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=1 38=4481 40=1 37=42348647 17=842698178 6=159.0825000 14=200 60=20151119-17:14:16 75=20151119 10=020 )
#!/usr/bin/perl
use strict ;
use warnings ;
my $filename = "/tmp/foo" ;
open(my $fh, '<', $filename)
or die "Could not open file '$filename' $!";
my @fixString = " ";
while (my $row = <$fh>) {
chomp $row;
my @line = split("\s", "$row");
foreach my $line(@line) {
if ($line =~ /56=(\w+)/ ) {
$fixString[3] = "$1" ;
}
foreach $line(@line) {
if ($line =~ /49=(\w+)/ ) {
$fixString[2] = "From $1 to " ;
}
#sleep 1 ;
}
}
print "@fixString\n" ;
}
出于某种原因当我使用&#34; \ s&#34;作为分裂者,它分裂了&#34; s&#34; - 我认为空间的正则表达式是&#34; \ s&#34;
From TWORDERS42 to ca
From TWORDERS42 to USA4P3_42
From USA4P3_42 to TWORDERS42
From ca to TWORDERS42
From USA4P3_42 to TWORDERS42
From ca to TWORDERS42
From USA4P3_42 to TWORDERS42
From ca to TWORDERS42
From USA4P3_42 to TWORDERS42
From ca to TWORDERS42
From USA4P3_42 to TWORDERS42
From ca to TWORDERS42
From USA4P3_42 to TWORDERS42
From USA4P3_42 to TWORDERS42
From ca to TWORDERS42
From ca to TWORDERS42
From USA4P3_42 to TWORDERS42
当我更换&#34; \ s&#34;用&#34; &#34;它变得更好 -
From TWORDERS42 to casperbank
From TWORDERS42 to USA4P3_42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
我想要的是打印输出之间的一致空间。从阵列打印时有没有办法做到这一点?
From TWORDERS42 to casperbank
From TWORDERS42 to USA4P3_42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
From USA4P3_42 to TWORDERS42
From casperbank to TWORDERS42
From casperbank to TWORDERS42
From USA4P3_42 to TWORDERS42
答案 0 :(得分:2)
要以一致的方式打印,您可以使用printf:
printf("%-5s %-11s %s %s", $line[0], $line[1], $line[2], $line[3]);
通过这种方式,您可以引入必要的空白区域来实现它们。或者你可以做print join("\t" @line);
答案 1 :(得分:1)
您的代码中存在一些错误,效率低下,冗余等,实际上只需恢复到Perl的默认行为和/或快捷方式即可解决这些错误。默认情况下,许多内置函数在$_
上运行,我们可以利用它来简化和缩短代码。更进一步,split()
,当提供' '
模式(或根本没有模式!)时,会自动将模式视为/\s+/
。我还使用哈希来存储我们找到的任何key=value
对,如果我们决定要访问不同的数据部分,这将为我们提供灵活性。
总而言之,这是解决问题的另一种方法:
use strict;
use warnings;
while (<DATA>) {
chomp;
my @fields = split;
my %values;
for (@fields) {
my @parts = split(/=/);
next unless scalar(@parts) == 2;
$values{$parts[0]} = $parts[1];
}
printf("From %-13s to %s\n", $values{49}, $values{56});
}
__DATA__
2015-11-19 12:11:34,050INFO [Fix message assembler]TWORDERS42_UBS-<1356 OrderSingle (8=FIX.4.2 9=195 35=D 34=1356 49=TWORDERS42 52=20151119-17:11:34.049 56=UBS 57=PROG 97=N 115=GUARDIAN 11=AFMC286;6CE7-7DF5 15=USD 21=1 22=1 38=4481 40=1 48=031162100 54=1 55=AMGN 59=0 60=20151119-17:01:11 92=CSA 10=028 )
2015-11-19 12:11:34,050INFO [SessionManager for CamUSP3: TWORDERS42 UBS]USA4P3_42_CamUSP3_42->4735 OrderSingle (8=FIX.4.2 9=197 35=D 34=4735 49=TWORDERS42 52=20151119-17:11:34 56=USA4P3_42 57=PROG 97=N 115=GUARDIAN 11=AFMC286;6CE7-7DF5 15=USD 21=1 22=1 38=4481 40=1 48=031162100 54=1 55=AMGN 59=0 60=20151119-17:01:11 92=CSA 10=210 )
2015-11-19 12:12:54,468INFO [Fix message assembler]USA4P3_42_CamUSP3_42-<68210 ExecutionReport (8=FIX.4.2 9=285 35=8 34=68210 52=20151119-17:12:54 43=N 49=USA4P3_42 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4481 150=0 11=AFMC286;6CE7-7DF5 31=0.0000 32=0 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=0 38=4481 40=1 37=42348647 17=842696664 6=0.0000000 14=0 60=20151119-17:12:54 75=20151119 10=099 )
2015-11-19 12:12:54,468INFO [SessionManager for CamUSP3: USA4P3_42 CamUSP3_42]TWORDERS42_UBS->9012 ExecutionReport (8=FIX.4.2 9=278 35=8 34=9012 52=20151119-17:12:54 43=N 49=UBS 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4481 150=0 11=AFMC286;6CE7-7DF5 31=0.0000 32=0 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=0 38=4481 40=1 37=42348647 17=842696664 6=0.0000000 14=0 60=20151119-17:12:54 75=20151119 10=181 )
2015-11-19 12:13:58,300INFO [Fix message assembler]USA4P3_42_CamUSP3_42-<68476 ExecutionReport (8=FIX.4.2 9=303 35=8 34=68476 52=20151119-17:13:58 43=N 49=USA4P3_42 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4381 150=1 11=AFMC286;6CE7-7DF5 29=1 31=159.0800 32=100 30=O 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=1 38=4481 40=1 37=42348647 17=842697795 6=159.0800000 14=100 60=20151119-17:13:58 75=20151119 10=243 )
2015-11-19 12:13:58,300INFO [SessionManager for CamUSP3: USA4P3_42 CamUSP3_42]TWORDERS42_UBS->9061 ExecutionReport (8=FIX.4.2 9=296 35=8 34=9061 52=20151119-17:13:58 43=N 49=UBS 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4381 150=1 11=AFMC286;6CE7-7DF5 29=1 31=159.0800 32=100 30=O 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=1 38=4481 40=1 37=42348647 17=842697795 6=159.0800000 14=100 60=20151119-17:13:58 75=20151119 10=068 )
2015-11-19 12:14:16,245INFO [Fix message assembler]USA4P3_42_CamUSP3_42-<68598 ExecutionReport (8=FIX.4.2 9=304 35=8 34=68598 52=20151119-17:14:16 43=N 49=USA4P3_42 50=PROG 56=TWORDERS42 128=GUARDIAN 20=0 151=4281 150=1 11=AFMC286;6CE7-7DF5 29=2 31=159.0850 32=100 30=65 21=1 15=USD 59=0 55=AMGN 22=1 48=031162100 54=1 39=1 38=4481 40=1 37=42348647 17=842698178 6=159.0825000 14=200 60=20151119-17:14:16 75=20151119 10=020 )
输出:
From TWORDERS42 to UBS
From TWORDERS42 to USA4P3_42
From USA4P3_42 to TWORDERS42
From UBS to TWORDERS42
From USA4P3_42 to TWORDERS42
From UBS to TWORDERS42
From USA4P3_42 to TWORDERS42
这适用于内联数据,这对于示例非常有用,但对于现实生活来说并不是那么好。好吧,Perl也可以轻松处理文件:只需删除底部的__DATA__
部分,然后将while (<DATA>)
更改为while (<>)
。
然后您可以像这样运行脚本:
perl myscript.pl file1.log file2.log file3.log [...]
如果你希望第一列的宽度是高度动态的,你可以做这样的事情,但我认为这个例子太过分了,我宁愿只使用“工程判断”。
use List::Util qw(max);
my @data;
while (<DATA>) {
chomp;
my @fields = split;
my %values;
for (@fields) {
my @parts = split(/=/);
next unless scalar(@parts) == 2;
$values{$parts[0]} = $parts[1];
}
push(@data, \%values);
}
my $max = max(map { length($_->{49}) } @data);
for (@data) {
printf("From %-*s to %s\n", $max + 3, $_->{49}, $_->{56});
}