-----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 31, 2004 6:50 AM To: [EMAIL PROTECTED] Subject: delete all lines in a file save it come out
Hi , I have a problem in deleting all the lines in a file and saving it . Actually my log file keep appending all the messages for that i need to clean it up i.e delete all lines in it save it . when i do this initially file shows zero bytes , but as soon as the next message appends ,, file sizes jumps to the actual size and not from zero . I tried this with flock() option also . for Somereasons it is not working I could see some junk characters like this in the file [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ Because of these characters, file size will not go to zero bytes. [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ [EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@^ here is my script... Can somebody help on this .. for my $logfile (@filelist) { $lines = 0; open(FILE, "> $logfile") or die "Couldn't open $logfile : $!\n"; # This logfile keeps appending in a linux m/c flock(FILE,2); while (sysread FILE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); } print FILE Sudhakar; flock(FILE,8); close(FILE); print "No. of lines in $logfile", $lines, "\n"; # system "vi $logfile +delete$lines +wq"; # Delete all the lines in the file ... This command will never work for me. Dont know } Sudhakar Gajjala Well if all you want to do is count the number of lines in the file then zero out the file, the easiest way that I can think of would be to use a couple of system calls like this. $logfile = myfile.txt; $numlines = system(`cat $logfile |wc -l`)|| die "Cannot get number of lines\n"; print $numlines; system(`cp /dev/null $logfile`) || die "Cannot zero load file\n"; Of course since you seem to have a list of files you will have to modify this, but the general idea is there. And of course this script is dependent on your machine being a unix box, which you didn't mention what kind of OS you were running. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>