Issue
I'm writing a script to get a list of only the commands from my crontab. My input data so far looks like this:
$ crontab -l | grep -i --color -v '^#' | awk '$0 ~ /^*\/[0-9]{1,20}|\*+/'
*/5 * * * * /usr/local/bin/bash msmtp-queue -r
*/5 * * * * /usr/bin/env bash /Users/tonybarganski/bin/offlineimap-fetch
*/15 * * * * /usr/bin/env bash ~/bin/checkMail
00 12 * * * /usr/local/bin/bash ~/bin/lbdb-fetchaddress-daily
* */3 * * * /usr/bin/env rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
Outcome
Print everything after either '/usr/bin/env bash' or '/usr/local/bin/bash' or '* */3 * * *'
msmtp-queue -r
offlineimap-fetch
checkMail
lbdb-fetchaddress-daily
rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
Tried
I tried starting from the end of file ($NF) and printing everything up until the first forward slash but I just got syntax errors with where to put the brackets:
$ crontab -l | grep -i --color -v '^#' | \
awk -F'/' '{ if ($0 ~ /^*\/[0-9]{1,20}|\*+/) for(i=NF;i>0;i--) { if ( sub(/\//, "", $i) { print $i} } }'
awk: cmd. line:1: { if ($0 ~ /^*\/[0-9]{1,20}|\*+/) for(i=NF;i>0;i--) { if ( sub(/\//, "", $i) { print $i} } }
awk: cmd. line:1: ^ syntax error
How can this be achieved?
Solution
If you don't really want the output in your question and instead want the output shown in the currently accepted answer then all you need is:
$ sed -E 's/([^ ]* ){5}//' file
/usr/local/bin/bash msmtp-queue -r
/usr/bin/env bash /Users/tonybarganski/bin/offlineimap-fetch
/usr/bin/env bash ~/bin/checkMail
/usr/local/bin/bash ~/bin/lbdb-fetchaddress-daily
/usr/bin/env rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
The above requires a sed that has -E
to enable EREs, e.g. GNU or BSD sed. With any POSIX sed you could do sed 's/\([^ ]* \)\{5\}//' file
instead or with any POSIX awk awk '{sub(/([^ ]* ){5}/,"")}1' file
.
Answered By - Ed Morton Answer Checked By - Gilberto Lyons (WPSolving Admin)