2014-10-11 110 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#]/測試如果該行以#或一個數字開始。如果測試成功,則打印該行。