模式匹配在cron

时间:2014-10-11 13:38:12

标签: regex bash grep

我想grep行以##*number#number开头。换句话说,我希望从cron命令的开头grep直到行尾。我不需要crontab标题。 这是crontab文件:

# /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只有我上面解释过的模式? 抱歉英文不好。

2 个答案:

答案 0 :(得分:1)

这是另一个sed变体,它以第一行开头打印,以第一行开头:

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

答案 1 :(得分:0)

你可以用sed:

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

-n选项会阻止自动显示行

6,$是一系列行(从第6行到最后一行)

/^[0-9#]/测试行是以#开头还是以数字开头。如果测试成功,p将打印该行。