Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

pattern matching only last line of a file and then copy 10 lines above till end

by ultibuzz (Monk)
on Oct 20, 2005 at 15:22 UTC ( #501697=perlquestion: print w/ replies, xml ) Need Help??
ultibuzz has asked for the wisdom of the Perl Monks concerning the following question:

hi all,

as u might see im quit new to perl

i want to make a script that looks every 30 mins in a file and when the last line and yes only the last line contain some special text it shoud copy 10 lines above + last line in a separate file

but i dont have a clue how to say that the apttern amtching only looks @ the last line and then to get 10 lines up in the file and print out from there to the end

i think i can manage it with some kind of array and counter but i have realy no clue

for any help links sugestions and so on i say thx

Comment on pattern matching only last line of a file and then copy 10 lines above till end
Re: pattern matching only last line of a file and then copy 10 lines above till end
by philcrow (Priest) on Oct 20, 2005 at 15:26 UTC
    You could use Tie::File. That let's you think of your file as an array. The last line is the last element in the array. It would look something like this:
    use Tie::File; my @array; tie @array, 'Tie::File', filename or die ...; if ( $array[-1] =~ /regex here/ ) { # print elemeents -10 to -1. }
    Phil
Re: pattern matching only last line of a file and then copy 10 lines above till end
by holli (Monsignor) on Oct 20, 2005 at 15:31 UTC
    use strict; use warnings; use File::ReadBackwards ; my $bw = File::ReadBackwards->new( 'file' ) or die "can't read 'log_file' $!" ; my @lines; $lines[0] = $bw->readline; if ( $lines[0] =~ /pattern/ ) { for (1..9) { $lines[$_] = $bw->readline; } } @lines = reverse @lines; print "@lines";


    holli, /regexed monk/
Re: pattern matching only last line of a file and then copy 10 lines above till end
by emilper (Novice) on Oct 20, 2005 at 15:47 UTC
    #!/usr/bin/perl -w # usefull on a unix-like system # this uses tail - displays the last lines in a file # 'tail -n 4 filename' shows the last 4 lines in a file my $file_to_check = '/tmp/test_file.txt'; my $file_to_store = '/tmp/fill_this_file.txt'; my $pattern = 'special text'; while (1) { my $last_line = `tail -n 1 $file_to_check`; if ($last_line =~ /$pattern/) { `tail -n 11 $file_to_check >> $file_to_store`; }; sleep(30 * 60); }
Re: pattern matching only last line of a file and then copy 10 lines above till end
by saskaqueer (Friar) on Oct 20, 2005 at 15:57 UTC

    Here's one way. Note that my example clobbers any existing output file and replaces that data with the new 10 lines. If you need to append the data to existing content, make sure to change that part of the script.

    One thing I noticed is how old fashioned I am when it comes to array indices; I prefer using $lines[($#lines - 10) .. $#lines] rather than $lines[-11 .. -1]. I guess I just don't like a negative index? I'm strange that way :)

    #!/usr/bin/perl -w use strict; my $in_file = 'in.dat'; my $out_file = 'out.dat'; my $spec_text = qr/special text here/; while (1) { open(my $fh_in, '<', $in_file) or die("open failed: $!"); my @lines = <$fh_in>; close($fh_in); if ($lines[$#lines] =~ $spec_text) { open(my $fh_out, '>', $out_file) or die("open failed: $!"); print $fh_out @lines[($#lines - 10) .. $#lines]; close($fh_out); } sleep 60 * 30; # 30 minutes }
Re: pattern matching only last line of a file and then copy 10 lines above till end
by ultibuzz (Monk) on Oct 20, 2005 at 23:02 UTC
    well what can i say thx to all of u i will try it tomorrow cous i got to work in 3 hours ;D and here it is 01:00 am ;D

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://501697]
Approved by neversaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-08-01 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (0 votes), past polls