Grep a string

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

Rajesh M. wrote in post #968280:

regular expression should only look for the string after digitals 0 6 *

  • 0, here it should look for shutdown -y -g0 -i6

Have you tried searching for an online regexp reference, or used a
regexp book? If so, which one?

Things like “how to match a digit or asterisk” and “how to match a
space” should be easy to find there.

Also: are you actually trying to do this with grep/egrep? Or do you want
to write something in ruby?

On Tue, Dec 14, 2010 at 2:32 PM, Rajesh M. [email protected] wrote:

0 10 * * 0 sync; sync; reboot <---- regex should look for only sync;
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 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

On Dec 14, 2010, at 8:32 AM, Rajesh M. wrote:

0 10 * * 0 sync; sync; reboot <---- regex should look for only sync;

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 B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

I think use sed is more convenient:
grep “sync;sync;reboot” cron* |head -1 | sed ‘s/[0-9*\]//g;s/^[ ]*//’
If you want to use ruby to shave the expression you can find what you
wanted in ruby regexp,it’s a piece of cake in ruby world.

Peter V. wrote in post #968289:

On Tue, Dec 14, 2010 at 2:32 PM, Rajesh M. [email protected] wrote:

0 10 * * 0 sync; sync; reboot <---- regex should look for only sync;
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 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

cat /tmp/cron.list | egrep ‘^[0-9*]’
00 01 * * 0 (sync ; sync ; sync ; /sbin/reboot)
00 18 * * 6 (sync; sync; sync; /sbin/reboot)

Not sure it is still getting digits, what am doing wrong here?

On Dec 14, 2010, at 11:20 AM, Rajesh M. wrote:

root cd / && run-parts --report /etc/cron.hourly

Not sure it is still getting digits, what am doing wrong here?


Posted via http://www.ruby-forum.com/.

cat /tmp/cron.list | ruby -nle ‘next if $_ =~ /^\s*#/; fields =
$_.split(" ",6); puts fields.inspect;’

Or:

cat /tmp/cron.list | ruby -nle ‘next unless $_ =~ /sync|reb|shut/;
fields = $_.split(" ",6); puts fields.inspect;’

Can you work with that? If not, then why do you keep asking on a Ruby
list?

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Tue, Dec 14, 2010 at 7:53 PM, Brian C. [email protected]
wrote:

Rajesh M. wrote in post #968323:

cat /tmp/cron.list | egrep ‘^[0-9*]’
00 01 * * 0 (sync ; sync ; sync ; /sbin/reboot)
00 18 * * 6 (sync; sync; sync; /sbin/reboot)

Not sure it is still getting digits, what am doing wrong here?

The part you may have overlooked is the ‘cut’ command at the end of
$ cat crontab | egrep ‘^[0-9*]’ | cut -f3-

‘egrep’ selects the relevant lines (but the complete lines)
‘cut’ cuts away a part of the lines (that where previously selected with
egrep)

Anyway, using one of the proposed ruby variants will be much simpler
(and more on-topic …).

Peter

Rajesh M. wrote in post #968323:

cat /tmp/cron.list | egrep ‘^[0-9*]’
00 01 * * 0 (sync ; sync ; sync ; /sbin/reboot)
00 18 * * 6 (sync; sync; sync; /sbin/reboot)

Not sure it is still getting digits, what am doing wrong here?

Grep shows the whole line which matches, or with the -o option shows the
part of the line which matches the pattern. But you want to show the
part of the line which doesn’t match the pattern.

That’s why people have been suggesting using sed, which edits the line:

sed ‘s/^[0-9* ]*//’ /tmp/cron.list

But who needs sed when you have ruby?

ruby -pe ‘sub! /^[\d* ]/,""’ /tmp/cron.list

However that line relies on the ugly Perlism of special default
operations on $_. Better to type a few more characters and be explicit:

cat /tmp/cron.list | ruby -ne ‘puts $1 if /^[\d* ]+(.*)/’

cat /tmp/cron.list | ruby -ne ‘puts $1 if /^[\d* ]+(.*)/ =~ $_’

On Dec 14, 2010, at 6:21 PM, Ryan D. wrote:

[“/5", "”, “", "”, ““, “~/Work/p4/zss/src/scripts/dev/
p4review.py”]
[”
/15”, “", "”, “", "”, “/Volumes/Users/www/zenspider.com/bin/
webupdate.sh”]
[“0”, “", "”, “", "”, “(imap_cleanse”, “&&”, “imap_flag)”, “&>”,
“/dev/null”]
[“12”, “", "”, “", "”, “cd”, “~/Work/git/synchronizer”, “&&”,
“rake”, “-s”, “sync”]

Except that I have $_.split(" ", 6) since there are 5 schedule parts
and 1 command part, I want all the command to be left together as a
single part. So my fields[-1] is effectively $F[5…-1]*’ ’ had I used
the -a. Good to remind the list of the flag though.

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Dec 14, 2010, at 07:21 , Rob B. wrote:

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;’

Don’t forget the -a flag!

% ruby -h | grep – -a
-a autosplit mode with -n or -p (splits $_ into $F)
-Fpattern split() pattern for autosplit (-a)

so you can do something like:

% ruby -nae ‘next unless $_ =~ /^[\d*]/; p $F’
crontab.envy.zenspider.com
[“/5", "”, “", "”, ““,
“~/Work/p4/zss/src/scripts/dev/p4autotest.sh”]
[”
/5”, “", "”, “", "”, “~/Work/p4/zss/src/scripts/dev/p4review.py”]
[“/15", "”, “", "”, “",
“/Volumes/Users/www/zenspider.com/bin/webupdate.sh”]
[“0”, "
”, “", "”, “", “(imap_cleanse”, “&&”, “imap_flag)”, “&>”,
“/dev/null”]
[“12”, "
”, “", "”, “*”, “cd”, “~/Work/git/synchronizer”, “&&”,
“rake”, “-s”, “sync”]