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

Hi all, I am new to PERL and needs some help.

I need a to run a UNIX script which telnets into a NT server and opens a file and checks each line and the date in the 4th columns of a CSV file and deletes any lines which are older than 14 Days. This script has to be trigged from a Unix box (1) per week.

And the CSV file is updated from another program and I need to stop it from having any conflict with this script. Maybe using flock.

Here is some sample data from the CSV :-

63547 VPNT O 25112004 0:07:48 Boom Gate Exit 63547 VPNT I 25112004 2:32:28 Boom Gate Entry

I think maybe I need to use a combination of PERL, CAT, GREP, CUT, AWK.

Any advice would be appreciated

Thanks

  • Comment on Help - Deleting a log line with a data older than 14 days

Replies are listed 'Best First'.
Re: Help - Deleting a log line with a data older than 14 days
by davido (Cardinal) on Dec 10, 2004 at 04:09 UTC

    Well, you really don't need Perl, cat, grep, cut, and awk all at the same time. One Perl script and a cron job can meet your requirements. One of the great things about Perl is the building blocks it provides natively, combined with those provided through the CPAN.

    I would use Tie::File. It has a flock() method. If you're using that, it's a good idea to load the Fcntl module so that you have access to the ':flock' constants. You don't have to use Tie::File, but it is convenient, and can handle the locking for you. You could also use DBI along with DBD::CSV if you want to treat the logfile like a database, but that seems like overkill. Or you could just run through the file line by line writing out a tempfile, and then rename the tempfile over the original logfile.

    Then use Text::CSV to grab the date field of each entry, one by one. Use Date::Manip (or some other similar module) to correctly parse and compare the date. If it's older than 14 days, delete that element from the tied array that represents the logfile.

    Set up a cron job to cause the script to run once a week.

    That's it, you're done.

    Let us know if you need help with some aspect of putting the building blocks together.


    Dave

Re: Help - Deleting a log line with a data older than 14 days
by Corion (Patriarch) on Dec 10, 2004 at 08:00 UTC

    If this process takes place on an NT machine, and the file is hosted on a Win32 file system (NTFS or any FAT system), then you will have a problem if the program writing to the logfile has a lock on it, because under Windows, respecting a file lock is not a consentual activity but file locks are enforced by the operating system. So you might have to stop the log writing program, truncate the log, and then restart the log writing program again.

    You might be lucky and the program maybe closes the file after each line written to it, so you could wait in a tight loop to move the file away if you get lock violations when opening the file.

Re: Help - Deleting a log line with a data older than 14 days
by rrwo (Friar) on Dec 10, 2004 at 20:16 UTC

    That doesn't look like a CSV to me. Where are the commas?

    Also: any reason that you're not doing in directly on the Windows NT box? Perl runs quite well on NT/2000/2003, and when I used to maintain several NT servers for a living, most of my admin scripts were in Perl.

      Thanks Gods,

      Thank you soo much for taking the time to answer my question, Really good of you.

      Ok here is a bit from the CSV file, Yeah I could run it on the NT box, but the jobs has to be trigged from the Unix cron job. so I was thinking it might be better to do the whole thing on the unix box. But it would be ok to run it on the NT box as well..

      I just want to get the code down fist to process the file and then worry about that later. Yes I will need to read the data from the main file and save to another temp file then rename this to the original. File locking won’t be that much of a problem if I ran it about 2.30AM.

      Thanks for all the advice. I just started and didn’t thinking it would be this complicated. I was just going to use the cut,awk, etc.

      Any code to start me off or sample code of similar task would be appreciated.

      Can any one give me the some code direction as to how to process the line to check for that date and then remove the line and save to a file,etc. I realy thought I could use use CUT, grep,etc.

      0000063547,VPNT,O,25112004,00:07:48,Boom Gate Exit
      0000063547,VPNT,I,25112004,02:32:28,Boom Gate Entry
      0000070200,VPNT,I,25112004,05:45:33,TS 1 Entry
      0000066665,VPNT,I,25112004,05:47:49,VSG 1 Entry
      0000056910,VPNT,I,25112004,05:48:20,VSG 1 Entry
      0000073066,VPNT,I,25112004,05:52:26,TS 1 Entry
      0000069267,VPNT,I,25112004,05:59:17,TS 1 Entry
      0000070696,VPNT,I,25112004,06:04:16,Boom Gate Entry
      0000064635,VPNT,I,25112004,06:10:17,Boom Gate Entry
      0000068224,VPNT,I,25112004,06:10:46,TS 8A Entry
      0000067360,VPNT,I,25112004,06:15:10,TS 8B Entry
      0000069604,VPNT,I,25112004,06:15:19,VSG 1 Entry
      0000075836,VPNT,I,25112004,06:17:44,TS 8B Entry
      0000064645,VPNT,I,25112004,06:23:32,VSG 1 Entry
      0000073744,VPNT,I,25112004,06:23:38,TS 8A Entry
      0000067516,VPNT,I,25112004,06:23:52,TS 1 Entry