regex - 注释行与 Ansible 匹配一些模式和后面的 n 个字符串
问题描述
我需要注释匹配模式的行和后面的 n 行(如果行首已经没有注释符号)。当我需要评论任何包含模式“重启”的行时,我会这样做:
- name: Excluding reboot command from script
replace:
path: "{{some_path}}/someshellscript.sh"
regexp: '(^(?!.*#).*reboot.*)'
replace: '#\1'
此任务注释行带有“reboot”和正则表达式将不匹配“#reboot”。但是,如果我需要用 'reboot' 注释行,并且在这行还没有注释的情况下在它之后的两行注释呢?
解决方案
只要您可以可靠地预测您的行返回将是什么,并且在正则表达式匹配后您需要评论的行数不会有差异,您就可以使用它:
- name: Excluding reboot command from script
replace:
path: "{{some_path}}/someshellscript.sh"
regexp: '(^(?!.*#).*reboot.*\n)(.*\n)(.*)'
replace: '#\1#\2#\3'
推荐阅读
- javascript - dompdf - 如何在 chrome 浏览器中自动触发打印对话框
- javascript - 如何用vue制作对象数组?
- python - windows web-scraping编码错误:非专业本科,不知道怎么办
- c++ - POCO::PostgreSQL:如何将 std::vector 支持添加到`Binder::bind`?
- java - for 循环中的 if / else 是否可以转换/简化为 Java 8
- flutter - 需要将异步调用传递给firestore中的查询参数
- angular - 如何从角度错误对象中获取行号和文件名?
- python - Colab:如何加快训练速度?如何有效地将数据从谷歌驱动器加载到 Colab?
- wordpress - 301 使用通配符重定向到使用 htaccess 的单个页面
- python - 如何使用 Selenium 和 Python 更改 datepicker 的值