how to cut a text file in perl

J

Joseph

hi i have a log file that it is increasing to big, how can i keep this
file of
a certain size, in other words, how can i make sure that the file is
only 200 lines long and delete the first lines if longer. i think it
would be easier to write this in a shell script. but i have no idea
in perl. can anyone tell me how to print the last 200 lines of a file
and overwrite the ssame file. thanks.
 
B

boyd

Joseph said:
hi i have a log file that it is increasing to big, how can i keep this
file of
a certain size, in other words, how can i make sure that the file is
only 200 lines long and delete the first lines if longer. i think it
would be easier to write this in a shell script. but i have no idea
in perl. can anyone tell me how to print the last 200 lines of a file
and overwrite the ssame file. thanks.

There are many ways. A straight-forward way:
Let the logfile be named 'log'.

open I, "<log";
my @lines = <I>;
close I;

open O, ">log";
my $numlines = scalar @lines;
for my $line(@lines[$numlines-200 .. $numlines-1]) {
print O $line;
}

Boyd
 
K

Keith Keller

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hi i have a log file that it is increasing to big, how can i keep this
file of
a certain size, in other words, how can i make sure that the file is
only 200 lines long and delete the first lines if longer. i think it
would be easier to write this in a shell script. but i have no idea
in perl. can anyone tell me how to print the last 200 lines of a file
and overwrite the ssame file. thanks.

This isn't a Perl answer, but you should investigate logrotate.

- --keith

- --
(e-mail address removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAM4kNhVcNCxZ5ID8RAm3JAJ0TH1mvG1cnYrosKlbRg47AXIsHVgCfV3iE
sRYSOdqn472Nr1Yi/Fak7Mw=
=zlNJ
-----END PGP SIGNATURE-----
 
B

Brian McCauley

hi i have a log file that it is increasing to big, how can i keep this
file of
a certain size, in other words, how can i make sure that the file is
only 200 lines long and delete the first lines if longer. i think it
would be easier to write this in a shell script. but i have no idea
in perl. can anyone tell me how to print the last 200 lines of a file
and overwrite the ssame file.

Deja vu anyone?

I'm curious: what keywords did you try in your Usenet search engine
before you concluded this has not already been covered?

I tried:

http://groups.google.com/groups?q="log+file"+group:comp.lang.perl.*

The second hit I got was:

http://groups.google.com/[email protected]

Where, about 3 weeks ago, essentially the same question was asked.

Here's what Uri said:

you have an interesting problem. the trouble is that your log
generator/server is still writing to the file at the seek point it
knows about. so it may leave a major gap in the file if you delete the
earlier chunk. or your server could be reopening the log file each
time which solves the first problem but you then have a race condition
regarding the removal of the early entries. but you could then copy
the older entries to a new file and rename it to the old file which is
atomic. this only works if the server reopens the file. many servers
have a way to trigger them to rotate a log file i.e. close the current
one, rename it (with some date or cycle suffix) and open a new empty
log file.

so it all comes down to how the server is managing and writing to the
log file. we would need more info on that before a proper solution can
be discussed.


--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
T

Trent Curry

Joseph said:
hi i have a log file that it is increasing to big, how can i keep this
file of
a certain size, in other words, how can i make sure that the file is
only 200 lines long and delete the first lines if longer. i think it
would be easier to write this in a shell script. but i have no idea
in perl. can anyone tell me how to print the last 200 lines of a file
and overwrite the ssame file. thanks.

You could always make a call to
`tail -n 200 myfile.txt`

That is, if you are only doing this for a Unix based system. Other wise
boyd's code is lot more portable, and probably a better way.


--
Trent Curry

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'
 
T

toylet

actually, this is to _truncate_ a log.
hi i have a log file that it is increasing to big, how can i keep this
file of
a certain size, in other words, how can i make sure that the file is
only 200 lines long and delete the first lines if longer. i think it
would be easier to write this in a shell script. but i have no idea
in perl. can anyone tell me how to print the last 200 lines of a file
and overwrite the ssame file. thanks.
 
A

Anno Siegel

Please don't top-post. I moved your reply to the bottom.
actually, this is to _truncate_ a log.

What is this supposed to mean, and how is it useful?

Anno
 
T

toylet

hi i have a log file that it is increasing to big, how can i keep this
What is this supposed to mean, and how is it useful?

just a terminology thing. could help in google.com search.
 
B

Beable van Polasm

hi i have a log file that it is increasing to big, how can i keep
this file of a certain size, in other words, how can i make sure
that the file is only 200 lines long and delete the first lines if
longer. i think it would be easier to write this in a shell
script. but i have no idea in perl. can anyone tell me how to print
the last 200 lines of a file and overwrite the ssame file. thanks.


Here is a Perl program which will write to a logfile, and not let
it get bigger than 200 lines. Of course, there are lots of other
ways to do it.

#!/usr/bin/perl

use strict;
use warnings;
use Tie::File;

run();
sub run
{
# open logfile
# please read "perldoc Tie::File"
my $logfile = "logfile.txt";
my @logarray = ();
tie @logarray, "Tie::File", $logfile or die "couldn't tie to $logfile: $!";

while(rand(100) > 1)
{
# do something
print("LA LA LA!\n");

# pass a reference to the logarray to logmsg()
# see "perldoc perlreftut" and "perldoc perlref"
logmsg(\@logarray, "I did something!");
}

# untie array when finished
untie @logarray;
}

sub logmsg
{
my $logarrayref = shift;
my $msg = shift;

my $log = time . ": " . $msg;

# write to logfile
# see "perldoc -f push", and "perldoc perlreftut"
push @{$logarrayref}, $log;

# cut the logfile down to 200 lines if necessary.
while(scalar(@{$logarrayref}) > 200)
{
# see "perldoc -f shift", "perldoc perlref", and "perldoc perlfunc"
shift @{$logarrayref};
}
}

__END__

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top