Cron:Diaplay使用awk由Cron调度程序运行的命令

发布于 2025-02-02 03:26:10 字数 1465 浏览 2 评论 0原文

我正在编写一个脚本以获取我的crontab命令的列表。 到目前为止,我的输入数据看起来像这样:

$  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

结果

打印在'/usr/bin/env bash'或'/usr/usr/local/bin/bash'或' * * * * * * * * * * * * * * * * * * * *'

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

我尝试过

我尝试从该开始的所有内容。文件的结尾($ nf)并打印所有内容,直到第一个前向斜线,但是我只是在何处遇到了语法错误:

$ 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

如何实现这一目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光沙漏 2025-02-09 03:26:11

如果你想

在'/usr/bin/env bash'或
'/usr/local/bin/bash'或' * * * * * * * *'

且鉴于示例的格式,您可以在不同的模式上拆分,并检查至少有2个部分。

如果文件包含示例数据:

awk '
{
  n = split($0,a, /(^\* \*\/3 \* \* \*|\/usr\/local\/bin\/bash|\/usr\/bin\/env[[:space:]]+bash)[[:space:]]+/)
  if (n > 1) {
    print a[2]
  }
}
' file

输出

msmtp-queue -r 
/Users/tonybarganski/bin/offlineimap-fetch
~/bin/checkMail
~/bin/lbdb-fetchaddress-daily
/usr/bin/env rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log

If you want to

Print everything after either '/usr/bin/env bash' or
'/usr/local/bin/bash' or '* */3 * * *'

and given the format of the examples, you can split on the different patterns and check if there are at least 2 parts.

If file contains the example data:

awk '
{
  n = split($0,a, /(^\* \*\/3 \* \* \*|\/usr\/local\/bin\/bash|\/usr\/bin\/env[[:space:]]+bash)[[:space:]]+/)
  if (n > 1) {
    print a[2]
  }
}
' file

Output

msmtp-queue -r 
/Users/tonybarganski/bin/offlineimap-fetch
~/bin/checkMail
~/bin/lbdb-fetchaddress-daily
/usr/bin/env rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
迷荒 2025-02-09 03:26:11

仅获取我的crontab命令的列表

我只会抛弃5个第一个字段,让file.txt content进行

*/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.lo

输出

awk 'BEGIN{FS="[ ]"}{$1=$2=$3=$4=$5="";sub(/^ */,"");print}' file.txt

说明

/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.lo

:我通知gnu awk该字段分隔仪(fs)和输出字段分隔仪()是单个空间。我不使用默认的fs,因为它会导致更改选项卡字符为空间字符以使空间和压缩空间(2个或多个连续的空间进入单个空间)。对于每一行,我将第一,第2,第3,第4和第5个字段设置为空字符串,然后使用sub刮去领先的空间,然后print此类更改的行。

(在GNU AWK 5.0.1中测试)

get a list of only the commands from my crontab

I would just jettison 5 first fields, let file.txt content be

*/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.lo

then

awk 'BEGIN{FS="[ ]"}{$1=$2=$3=$4=$5="";sub(/^ */,"");print}' file.txt

gives output

/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.lo

Explanation: I inform GNU AWK that field separator (FS) and output field separator (OFS) is single space. I do not use default FS as it would cause changing tab characters to space characters to space and compressing spaces (2 or more consecutive spaces into single space). For each line I set 1st, 2nd, 3rd, 4th and 5th field to empty string, then use sub to shave off leading spaces, then print such altered line.

(tested in GNU Awk 5.0.1)

旧故 2025-02-09 03:26:11

如果您真的不希望您的问题中的输出,而是希望当前接受的答案中显示的输出,那么您需要的是:

$ 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

以上需要一个具有-e的SED来启用ERES,例如GNU或BSD SED。使用任何posix sed,您都可以做's/\([^]* \)\ {5 \} //'file或任何posix awk awk'{sub(sub(/) ([^]*){5}/,“”)} 1'文件

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.

套路撩心 2025-02-09 03:26:11

使用sed(如果适用)

$ sed s'/.*\(bin\/\|\/env \)\?bash \|.*\* //' input_file
msmtp-queue -r
/Users/tonybarganski/bin/offlineimap-fetch
~/bin/checkMail
~/bin/lbdb-fetchaddress-daily
/usr/bin/env rsync -auv -f- index.lock --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log

Using sed (if applicable)

$ sed s'/.*\(bin\/\|\/env \)\?bash \|.*\* //' input_file
msmtp-queue -r
/Users/tonybarganski/bin/offlineimap-fetch
~/bin/checkMail
~/bin/lbdb-fetchaddress-daily
/usr/bin/env rsync -auv -f- index.lock --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
分開簡單 2025-02-09 03:26:11
$ awk -F"bash|env bash|env" '!/^#/ {print $2}' <(crontab -l)
 msmtp-queue -r 
 /Users/tonybarganski/bin/offlineimap-fetch
 ~/bin/checkMail
 ~/bin/lbdb-fetchaddress-daily
 rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
$ awk -F"bash|env bash|env" '!/^#/ {print $2}' <(crontab -l)
 msmtp-queue -r 
 /Users/tonybarganski/bin/offlineimap-fetch
 ~/bin/checkMail
 ~/bin/lbdb-fetchaddress-daily
 rsync -auv -f"- index.lock" --exclude={'Macintosh HD','MyArchive'} ~/mnt/ ~/.Backup/mnt/ > ~/crypto-vols-backup.log
沧笙踏歌 2025-02-09 03:26:11

如果您想考虑潜在的选项卡:

mawk 'BEGIN { OFS=_;__=_="[^"(_=" \\t]")"*["_; gsub("...",_,_); FS="^"(_)__ } NF=NF'

 or

{n,g}awk 'BEGIN { OFS=_;FS="^("(_="[^"(_=" \\t]")"*["_)"){5}" } NF=NF'
 

- - - - -

/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

如果标签不关心,那么更简单:

{n,g}awk 'NF=NF' FS='^([^ ]+[ ]+){5}' OFS=

If you want to account for potential tabs :

mawk 'BEGIN { OFS=_;__=_="[^"(_=" \\t]")"*["_; gsub("...",_,_); FS="^"(_)__ } NF=NF'

 or

{n,g}awk 'BEGIN { OFS=_;FS="^("(_="[^"(_=" \\t]")"*["_)"){5}" } NF=NF'
 

—————

/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

if tabs are no concern then even simpler :

{n,g}awk 'NF=NF' FS='^([^ ]+[ ]+){5}' OFS=
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文