1753404 Members
7251 Online
108793 Solutions
New Discussion юеВ

crontab filter

 
SOLVED
Go to solution
wojtek75
Frequent Advisor

crontab filter

Hi,

my crontab is pretty long and complex and I would like to get non-commented lines which have day and month other than '*'. Any oneline script to perform it?
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: crontab filter

crontab -l |grep -v "#"


Pete

Pete
wojtek75
Frequent Advisor

Re: crontab filter

This only filters commented lines. What about picking the ones with day and month (3rd and 4th position of crontab entry) other than '*'?
James R. Ferguson
Acclaimed Contributor
Solution

Re: crontab filter

Hi:

# crontab -l | perl -nale 'next if m{^\s*(#|$)};print if $F[2] !="*" and $F[3] !="*"'

Perl counts fields zero-relative, so the monthday and month are field two (2) and three (3).

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: crontab filter

Hi (again):

ENOCOFFEE. The '!=' operator should have been 'ne' since we are comparing strings, not numbers. Hence:

# crontab -l | perl -wnale 'next if m{^\s*(#|$)};print if $F[2] ne "*" and $F[3] ne "*"'

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: crontab filter

You can also use awk:
crontab -l | awk '
substr($0, 1, 1) != "#" && NF >= 6 && $3 != "*" && $4 != "*" { print $0 }'