【发布时间】:2021-07-02 06:21:34
【问题描述】:
我想在我的一些脚本中添加一些客户标志,以便在打包之前通过 shell 脚本对其进行解析。
比方说,删除所有的多行文本
^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_BEGIN[_]+\n
之间
^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_END[_]+\n
我希望它具有容错性(关于“_”的数量),这就是我使用正则表达式的原因。
例如:
before.foo
i want this
#____NOT_FOR_CUSTOMER_BEGIN________
not this
nor this
#________NOT_FOR_CUSTOMER_END____
and this
//____NOT_FOR_CUSTOMER_BEGIN__
not this again
nor this again
//__________NOT_FOR_CUSTOMER_END____
and this again
会变成:
after.foo
i want this
and this
and this again
我宁愿使用 sed,但欢迎任何聪明的解决方案 :)
类似这样的:
cat before.foo | tr '\n' '\a' | sed -r 's/([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_BEGIN[_]+\a.*\a([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_END[_]+\a/\a/g' | tr '\a' '\n' > after.foo
【问题讨论】:
-
哪种工具/编程语言?
-
shell脚本,谢谢
-
不是shell而是
^(?:#|//)_+NOT_FOR_CUSTOMER_BEGIN_+(?:\s.+)*?\R(?:#|//)_+NOT_FOR_CUSTOMER_END_+\s*regex101.com/r/Qj2T59/1 -
它确实在工作,但我怎么称呼它?
标签: python shell awk sed substitution