gpt4 book ai didi

python - 如何删除或替换两个模式之间的多行文本

转载 作者:行者123 更新时间:2023-12-03 22:52:54 24 4
gpt4 key购买 nike

我想在我的一些脚本中添加一些客户标志,以便在由 shell 脚本打包之前对其进行解析。
比方说,删除之间的所有多行文本^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_BEGIN[_]+\n和之间^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_END[_]+\n我希望它具有容错性(关于“_”的数量),这就是我使用正则表达式的原因。
例如:
之前.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
会变成:
后.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

最佳答案

sed处理这个问题的最简单工具,因为它可以删除从开始模式到结束模式的行:

sed -E '/_+NOT_FOR_CUSTOMER_BEGIN_+/,/_+NOT_FOR_CUSTOMER_END_+/d' file

i want this
and this
and this again
如果您正在寻找 awk解决方案那么这里是一个更简单的 awk :
awk '/_+NOT_FOR_CUSTOMER_BEGIN_+/,/_+NOT_FOR_CUSTOMER_END_+/{next} 1' file

关于python - 如何删除或替换两个模式之间的多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66969115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com