Grep a string

R

Rajesh M.

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
 
B

Brian Candler

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?
 
P

Peter Vandenabeele

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
 
A

Adrian Tepes

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.
 
R

Rob Biedenharn

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
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
R

Rajesh M.

Peter Vandenabeele wrote in post #968289:
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 <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



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?
 
R

Rob Biedenharn

Peter Vandenabeele wrote in post #968289:
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 <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



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?


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 Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
B

Brian Candler

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* ]+(.*)/ =~ $_'
 
P

Peter Vandenabeele

Rajesh M. wrote in post #968323:
=C2=A0cat /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 eg=
rep)

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

Peter
 
R

Ryan Davis

Well, you asked on a Ruby list so you're getting a Ruby answer from = me.
=20
crontab -l | ruby -nle 'next if $_ =3D~ /^\s*#/; fields =3D $_.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 $_ =3D~ /^[\d\*]/; p $F' =
crontab.envy.zenspider.com=20
["*/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"]
 
R

Rob Biedenharn

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"]

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 Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top