Итак, переместить строки в файле в отдельный файл на основе заголовка?
Думаю, что-то вроде этого поможет:
#!/usr/bin/env perl
use strict;
use warnings 'all';
#hash of output filehandles.
my %output_files;
#detect dupes
my %seen;
my $ko_num = 'NULL';
#<> is the 'magic' filehandle. You can either use it to iterate STDIN
#or take a list of file names on the command line (just like sed/grep etc.)
while ( my $line = <> ) {
#see if the line starts with 'ko':
if ( $line =~ m/(^ko\d+)/) {
$ko_num = $1;
#open a new file - for overwriting (so we only do this once)
open ( $output_files{$ko_num}, '>', $ko_num ) or die $! unless $output_files{$ko_num};
#skip printing - could write a header here instead.
next;
}
#look for a 'K' number.
if ( my ($K_id) = $line =~ m/ko:(K\d+)/ ) {
#skip it if we've already seen this combination of 'ko' number
#and k number.
next if $seen{$ko_num}{$K_id}++;
#print the output to this particular output file.
print {$output_files{$ko_num}} $line;
}
}
#close the filehandles.
close ( $_ ) for values %output_files;
Таким образом, вы можете запустить myscript.pl file1.txt file2.txt file3.txt, и он должен делать правильные вещи с возможностью масштабирования. Его даже не волнует, отдельные это файлы или единый поток.
Вы можете использовать systemd-cat
, например
systemd-cat cat /path/to/log
или
systemd-cat -t encapsulated_app_log tail -f /path/to/log