Some time ago I wrote the code below to sort my crontab file by time (see sub compare_crons for the sorting criteria). It's very crude, but it could work as a starting point for reading a crontab file in memory. Once you read it in, you can make any changes, write it to a new file, an run the crontab command on it.

This program leaves in each element of @db an array reference containing the 5 time fields, and a text element containing the current line, together with any comments that came before it. Of course, for making any modifications, this second element would have to be split further.

eval 'exec perl -x $0 ${1+"$@"}' # -*-perl-*- if 0; #!perl -w # Sort a crontab by time # Each entry (possibly commented) is moved as a block with all the # comment lines before it. # Diego Zamboni, March 21, 2001. Happy Spring! use strict; use vars qw($curblock $tf @fields @db); # Regex for a time field in crontab, which can be a number, sequence o +f numbers # or an asterisk $tf='(?:[*]|(?:\d+(?:-\d+)?)(?:,\d+(?:-\d+)?)*)'; while (<>) { $curblock.=$_; if (@fields=(/^\#?($tf)\s+($tf)\s+($tf)\s+($tf)\s+($tf)\s+(.*)/)) { push @db, [[@fields], $curblock]; $curblock=""; } } foreach (sort compare_crons @db) { print "$_->[1]"; warn "-----------\n" . join(" | ", @{$_->[0]})."\n"; } # compare by month, day, hour and minute. Ignore weekday sub compare_crons { my @fa=@{$a->[0]}; my @fb=@{$b->[0]}; return compare_tf($fa[3],$fb[3]) || compare_tf($fa[2],$fb[2]) || compare_tf($fa[1],$fb[1]) || compare_tf($fa[0],$fb[0]); } # Compare two cron fields. Asterisk considered as zero, otherwise # compare by the first number sub compare_tf { my ($na,$nb); $na=($_[0]=~/^(\d*)/)[0]||0; $nb=($_[1]=~/^(\d*)/)[0]||0; return ($na <=> $nb); }

--ZZamboni


In reply to Re: Manipulating crontab by ZZamboni
in thread Manipulating crontab by tune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.