Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Cleaning file

by Anonymous Monk
on Aug 21, 2006 at 20:11 UTC ( [id://568672]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Is there an easy and save way to do a clean on a file using perl? We have a log file that we need to empty every week but leave the file as is without overwritting it or renaming ..etc I was thinking of somthing like
open(file, .. ) or die { foreach $line (<file>) { # replace each line with empty line .. }
I know there got to be an easy way to do it in perl , I am trying to sechdule this to run as a cron on a windows machine . Any advice is apprecitated. thanks

Replies are listed 'Best First'.
Re: Cleaning file
by pileofrogs (Priest) on Aug 21, 2006 at 21:02 UTC

    If you just open the file in write mode and then close it, that should do what you want.

    open(my $handle, ">", $filename); close($handle)

    From perlfunc:

     
    If MODE is ’>’, the file is truncated and 
    opened for output, being created if necessary.  
    
    -Pileofrogs
Re: Cleaning file
by Fletch (Bishop) on Aug 21, 2006 at 20:17 UTC

    Open it for read-write access (see perlopentut), seek to the beginning of the file, and then truncate.

    (Of course were you on a real OS I'd suggest just cp /dev/null logfile, but I have no idea if Cygwin or using NUL: would accomplish the same thing).

Re: Cleaning file
by graff (Chancellor) on Aug 22, 2006 at 05:32 UTC
    I agree with this suggestion that pileofrogs provided. But I wonder if there might be complications for you...

    1. Rather than just truncating the log file, should you be "rotating" it (saving some amount of recent log history)? There are perl modules for log file rotation, but basically, it's just a matter of renaming the "current" log file at, say, the end of each day and creating a new empty log file (e.g. "foo.log" is renamed to "20060821.foo.log", and a new "foo.log" is created; you retain some number of daily logs, and each day you just delete the oldest one).

    2. Is the log file being written to by a process that runs continuously and keeps the log file open for output at all times? If so, could this interfere with a separate cron job that tries to open/truncate the log file? (I'm not a windows user, so I don't know from personal experience, but I've seen some discussions about how output file locking works on windows.)

    If the answer to 2 is "yes", you presumably have to end the process in question, do whatever needs to be done to the log file, and restart the process. Or else you have to reconfigure the process, if possible, so it opens and closes the log file appropriately, or handles daily log rotation as part of its normal behavior.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found