【发布时间】:2020-09-30 14:19:20
【问题描述】:
这是我正在阅读的文件,
#Log1
Time Src_id Des_id Address
0 34 56 x9870
2 36 58 x9872
4 38 60 x9874
6 40 62 x9876
8 42 64 x9878
#Log2
Time Src_id Des_id Address
1 35 57 x9871
3 37 59 x9873
5 39 61 x9875
7 41 63 x9877
9 43 65 x9879
这是我写的代码,我正在逐行阅读然后拆分它
#!usr/bin/perl
use warnings;
use strict;
my $log1_file = "log1.log";
my $log2_file = "log2.log";
open(IN1, "<$log1_file" ) or die "Could not open file $log1_file: $!";
open(IN2, "<$log2_file" ) or die "Could not open file $log2_file: $!";
my $i_d1;
my $i_d2;
my @fields1;
my @fields2;
while (my $line = <IN1>) {
@fields1 = split " ", $line;
}
while (my $line = <IN2>) {
@fields2 = split " ", $line;
}
print "@fields1\n";
print "@fields2\n";
close IN1;
close IN2;
我得到的输出
8 42 64 x9878
9 43 65 x9879
所需的输出
Time Src_id Des_id Address
0 34 56 x9870
2 36 58 x9872
4 38 60 x9874
6 40 62 x9876
8 42 64 x9878
9 43 65 x9879
Time Src_id Des_id Address
1 35 57 x9871
3 37 59 x9873
5 39 61 x9875
7 41 63 x9877
9 43 65 x9879
如果我使用 push(@fields1 , split " ", $line);,我会得到这样的输出,
Time Src_id Des_id Address 0 34 56 x9870 B 36 58 x9872 D 38 60 x9874 F 40 62 x9876 H 42 64 x9878
它应该打印整个数组但只打印最后一行? 同样在此之后,我需要按顺序比较日志和打印的“时间”部分,但不知道如何在 while 循环中同时运行两个数组? 请以不带任何模块的标准方式提出建议,因为我需要在其他服务器上运行它。
【问题讨论】:
-
cat log1.log log2.log | sort -n或许 -
这是一个单线器。
$ perl -e"print sort { $a <=> $b} grep /^\d/,<>" log1.log log2.log