Cron:Diaplay使用awk由Cron调度程序运行的命令
我正在编写一个脚本以获取我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你想
且鉴于示例的格式,您可以在不同的模式上拆分,并检查至少有2个部分。
如果
文件
包含示例数据:输出
If you want to
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:Output
我只会抛弃5个第一个字段,让
file.txt
content进行输出
说明
:我通知gnu
awk
该字段分隔仪(fs
)和输出字段分隔仪()是单个空间。我不使用默认的fs,因为它会导致更改选项卡字符为空间字符以使空间和压缩空间(2个或多个连续的空间进入单个空间)。对于每一行,我将第一,第2,第3,第4和第5个字段设置为空字符串,然后使用sub
刮去领先的空间,然后print
此类更改的行。(在GNU AWK 5.0.1中测试)
I would just jettison 5 first fields, let
file.txt
content bethen
gives output
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 usesub
to shave off leading spaces, thenprint
such altered line.(tested in GNU Awk 5.0.1)
如果您真的不希望您的问题中的输出,而是希望当前接受的答案中显示的输出,那么您需要的是:
以上需要一个具有
-e
的SED来启用ERES,例如GNU或BSD SED。使用任何posix sed,您都可以做's/\([^]* \)\ {5 \} //'file
或任何posix awkawk'{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:
The above requires a sed that has
-E
to enable EREs, e.g. GNU or BSD sed. With any POSIX sed you could dosed 's/\([^ ]* \)\{5\}//' file
instead or with any POSIX awkawk '{sub(/([^ ]* ){5}/,"")}1' file
.使用
sed
(如果适用)Using
sed
(if applicable)如果您想考虑潜在的选项卡:
- - - - -
如果标签不关心,那么更简单:
If you want to account for potential tabs :
—————
if tabs are no concern then even simpler :