From: Rob Biedenharn Date: 2010-12-15T00:21:31+09:00 Subject: Re: Grep a string On Dec 14, 2010, at 8:32 AM, Rajesh M. 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 > > > Please help. > Thanks in advance Well, you asked on a Ruby list so you're getting a Ruby answer from me. crontab -l | ruby -nle 'next if $_ =~ /^\s*#/; fields = $_.split(" ", 6); puts fields.inspect;' For your sample, I'll show the output from `echo "0 10 * * 0 sync; sync; reboot"` rather than `crontab -l` $ echo "0 10 * * 0 sync; sync; reboot" | ruby -nle 'next if $_ =~ /^ \s*#/; fields = $_.split(" ",6); puts fields.inspect;' ["0", "10", "*", "*", "0", "sync; sync; reboot"] You could change the first statement from: next if $_ =~ /^\s*#/; which skips comment lines, to something like: next unless $_ =~ /sync|reb|shut/; to only operate on those lines. -Rob Rob Biedenharn Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab@GaslightSoftware.com http://GaslightSoftware.com/