【发布时间】:2021-02-05 04:57:46
【问题描述】:
对于我遇到的特定问题,我有一个解决方案,但它很老套,根本不便携:
crontab -l > my-crontab
command='~/test.sh'
minute=1
hour=2
sed -i 's#[0-9]\+ [0-9]\+ [*]\+ [*]\+ [*]\+ '"$command"'#'"$minute"' '"$hour"' * * * '"$command"'#g' my-crontab
crontab my-crontab
如果我的 crontab 中有一行读取,例如,61 25 * * * ~/test.sh,上述解决方案将正确地用 1 2 * * * ~/test.sh 替换该 整个 行,我当然在其中进行了硬编码整行的格式。
那么,更一般地说,我怎样才能在未知位置找到包含已知字符串的任何行,然后仅替换该行的前两列?
测试用例:my-crontab 包含许多行,其中至少有一行看起来像以下之一:
61 25 (gibberish of unknown format that contains the string ~/test.sh and more)
* 25 (gibberish of unknown format that contains the string ~/test.sh and more)
61 * (gibberish of unknown format that contains the string ~/test.sh and more)
一个可行的通用解决方案将找到任何包含字符串 ~/test.sh 的行并替换前两列:
1 2 (gibberish of unknown format that contains the string ~/test.sh and more)
我已经接受了下面使用 sed 和扩展正则表达式的答案,但我仍然有兴趣了解使用 awk 列语法的解决方案,即 $1 $2
【问题讨论】:
-
请发布具有相关预期输出的可测试样本,将它们编辑到您的帖子中,不要将它们作为 cmets 或图像发布。谢谢。
-
我会在命令后添加
# autofoobar以使其易于识别,然后将其过滤掉并重新添加:{ crontab -l | grep -v autofoobar; echo "$minute $hour * * * $command # autofoobar"; } | crontab -