2014-10-11 113 views
-1

我想grep行以##*number#number开头。换句话说,我希望从cron命令开始到结束为止。我不需要crontab头。 这里是crontab文件:cron模式匹配

# /etc/crontab: system-wide crontab 
# Unlike any other crontab you don't have to run the `crontab' 
# command to install the new version when you edit this file 
# and files in /etc/cron.d. These files also have username fields, 
# that none of the other crontabs do. 

SHELL=/bin/sh 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 

# m h dom mon dow user command 
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) 

#*/56 * * * * root bash /media/data/minutely.sh 
#20 * * * * root bash /media/data/hourly.sh 
#* * * * * root bash /media/data/looping.sh 
* * * * * root bash /media/data/looping2.sh 
20 * * * * root bash /media/data/hourly2.sh 

而我想要的是:

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) 

#*/56 * * * * root bash /media/data/minutely.sh 
#20 * * * * root bash /media/data/hourly.sh 
#* * * * * root bash /media/data/looping.sh 
* * * * * root bash /media/data/looping2.sh 
20 * * * * root bash /media/data/hourly2.sh 
. 
. 
. 
. 
#until end of line 

我已经尝试:

cat /etc/crontab | grep '^[^#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) 

如何才能grep只能拍我已经在上面解释过了吗? 对不起,英语不好。

+0

使用,而不是'^ [#0-9]' – hjpotter92 2014-10-11 13:41:22

+0

对不起,先生,我有尝试,但输出仍给我的标题(从'#/ etc/crontab'直到'#没有其他crontabs做.') – 2014-10-11 13:43:16

回答

1

下面是一个开始,在以数字开头的第一个行打印另一sed的变种:

sed -n '/^[0-9]/,$p' /etc/crontab 
+0

谢谢你sir :) – 2014-10-11 15:26:16

0

可以使用sed做到这一点:

sed -n '6,${/^[0-9#]/p}' /etc/crontab 

-n选项可防止显示的行自动

6,$是一个范围内的行(从管线6到最后一行)

/^[0-9#]/测试如果该行以#或一个数字开始。如果测试成功,则打印该行。