python - 按注释排序 crontab -l
问题描述
如何crontab -l
在每行的最后一个哈希标记之后按字母顺序排序?
* * * * * curl -m 10 https://google.com # C comment
# * * * * * curl -m 10 https://google.com # A comment
* * * * * curl -m 10 https://google.com # D comment
* * * * * curl -m 10 https://google.com # E comment
* * * * * curl -m 10 https://google.com # Z comment
## * * * * * curl -m 10 https://google.com # B comment
解决方案
用于awk
在每一行前面加上最后一个字段,对这个新的“第一个”字段进行排序,然后从结果中删除这个“第一个”字段:
$ crontab -l | awk -F'#' '{print $NF"#"$0}' | sort -t'#' -k1,1 | cut -d'#' -f2-
# * * * * * curl -m 10 https://google.com # A comment
## * * * * * curl -m 10 https://google.com # B comment
* * * * * curl -m 10 https://google.com # C comment
* * * * * curl -m 10 https://google.com # D comment
* * * * * curl -m 10 https://google.com # E comment
* * * * * curl -m 10 https://google.com # Z comment
笔记:
- 这会将空白视为可排序字符,例如,
#<2_spaces>D
将在之前排序#<1_space>A
- 通过更多的编码,
sort|cut
可以将功能整合到awk
代码中
推荐阅读
- bootstrap-4 - 成功提交表单后弹出引导模式
- python - Pyinstaller & Selenium:调用chromedriver时,出现一个空白的chromedriver控制台窗口
- javascript - 如何在s3中压缩大文件然后下载
- python - 从每小时数据中获取每月最大值
- python - 自定义记录器如何仅在较低级别记录到标准输出时记录错误/对标准错误至关重要?
- ios - 如何更新不同集合中的多个文档 Firestore 文档
- .net - Kafka Producer 调用 ProduceAsync 时内存泄漏
- excel - 正则表达式从 Outlook 电子邮件正文中提取金额
- php - 如何在 nginx 中为 PHP/Laravel 更改请求路径?
- qt - QML 对话框导致 Android 上的冻结(仅)。瞬态父级不能与窗口相同