On Tue, Dec 14, 2010 at 2:32 PM, Rajesh M. <munikur / gmail.com> wrote:
> Hi list,
>
> I need to grep the entire string "sync; sync; reboot" after 0 10 * * 0
> in crontab -l and store it in a variable, that string is different on
> different systems.
>
>
>
> crontab -l |egrep -i "(sync|reb|shut)"
>
> 0 10 * * 0 sync; sync; reboot <---- regex should look for only sync;
> sync; reboot
>
> cd /var/log
> grep "sync; sync; reboot" cron* |head -1
> cron:Dec 12 10:00:00 hostname CROND[14898]: (root) CMD (sync; sync;
> reboot)
>
> Regular expression should find any string without the digitals (0 10 * *
> 0)
>
> For example in another system if the crontab entry is like:
> 0 6 * * 0 shutdown -y -g0 -i6
> regular expression should only look for the string after digitals 0 6 *
> * 0, here it should look for shutdown -y -g0 -i6

If on the commandline, 'cut' may be useful.

$ cat crontab | egrep '^[0-9\*]'
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.monthly )

$ cat crontab | egrep '^[0-9\*]' | cut -f3-
root    cd / && run-parts --report /etc/cron.hourly
root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
root	test -x /usr/sbin/anacron || ( cd / && run-parts --report
/etc/cron.weekly )
root	test -x /usr/sbin/anacron || ( cd / && run-parts --report
/etc/cron.monthly )

'cut' splits by default on <TAB> and there seem to be exactly 2 TAB's
before the 'root'
part (you can use $ od -ax crontab  to check that in detail).

HTH,

Peter