and today is also the day I learnt whatif ($out =~/$last(.*)$/s
are as modifiers. I never had a real need to think about these use cases so far and I had to study better the documentation. Thanks again UPDATE: here a snippet (it seems working now)//m //s //ms
where a live fileB is for example this:1 #!/usr/bin/env perl 2 3 my $fileA = 'fileA.txt'; # the file to store incremental greps o +n fileB 4 my $fileB = 'fileB.txt'; # the live file which is log-rotated ev +ery 10MB 5 my $pattern = 'xxxx'; 6 7 #-------------------------------- 8 sub main { 9 $out = qx/grep -A 1 -B 1 $pattern $fileB/; 10 $out && writeA (\$out); 11 } 12 #-------------------------------- 13 sub write_file { 14 my ($file_name, $content_ref, $write_mod_append) = @_; 15 my $write_mod = $write_mod_append ? '>>' : '>'; 16 open(my $fh, $write_mod, $file_name) or die "Could not open fi +le '$file_name' $!"; 17 print $fh $$content_ref; 18 close $fh; 19 } 20 #-------------------------------- 21 sub writeA { 22 my ($out_ref) = @_; 23 my $write_mod_append; 24 if ( -e $fileA ) { 25 $write_mod_append = 1; 26 my $last_line = qx/tail -1 $fileA/; 27 chomp $last_line; 28 29 if ($$out_ref =~/$last_line(.*)$/s) { 30 $$out_ref = $1 31 } 32 } 33 write_file ($fileA, $out_ref, $write_mod_append) if ($$out_ref + =~ /\S/); 34 } 35 #-------------------------------- 36 main;
A first grep -A 1 -B 1 $pattern $fileB will be saved as fileA--------------...------------ --------------...------------ --------------aaa------------ -------------xxxx------------ --------------bbb------------ --------------...------------ --------------...------------ --------------ccc------------ -------------xxxx------------ --------------ddd------------ --------------...------------
After some time fileB can contain some more data (it's a live log) or it can be completely overwritten (log rotated on itself after 10MB)--------------aaa------------ -------------xxxx------------ --------------bbb------------ -- -- --------------ccc------------ -------------xxxx------------ --------------ddd------------
A second grep -A 1 -B 1 $pattern $fileB, will find 3 occurrences, but actually the <NEW> one is only the last xxxx. If I'd then append the grep output as it is, I will have 2 duplicates. I cannot just overwrite fileA, because when B is log rotated, the previous greps would be lost, not stored in my incremental fileA which should look like:--------------...------------ --------------...------------ --------------aaa------------ -------------xxxx------------ --------------bbb------------ --------------...------------ --------------...------------ --------------ccc------------ -------------xxxx------------ --------------ddd------------ --------------...------------ --------------...------------ --------------eee------------ -------------xxxx------------ --------------fff------------ --------------...------------
--------------aaa------------ -------------xxxx------------ --------------bbb------------ -- -- --------------ccc------------ -------------xxxx------------ --------------ddd------------ -- -- --------------eee------------ -------------xxxx------------ --------------fff------------
In reply to Re^2: extract the tail from a string (with new lines) containing a substring
by jjmoka
in thread extract the tail from a string (with new lines) containing a substring
by jjmoka
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |