我想在一些脚本中添加一些客户标志,以便在由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
^(?:#|//)_+NOT_FOR_CUSTOMER_BEGIN_+(?:\s.+)*?\R(?:#|//)_+NOT_FOR_CUSTOMER_END_+\s*
。https://regex101.com/r/Qj2T59/1 - The fourth bird