输出所有符合条件的段落
正则表达式本身就能工作 本身可用(它匹配它在其中找到 ^font\.size = 12\.0$ 行的段落),但我还没想清楚如何得到一个合适的Perl可执行命令来复现结果。
我当前正在运行
perl -lne 'BEGIN{undef $/;} print while /(^\S.*\n)+^font\.size = 12\.0(\n^\S.*)+/gm' teststring.txt
但它只是把整个文件打印出来。
如何让Perl的行为像 Regex101 一样?
Regex
/(^\S.*$\n)+^font\.size = 12\.0$(\n^\S.*$)+/gm
teststring.txt
# bbox.tolerance = 1e-5
[[heading]]
# Complex Plane
level = 1
greedy = true
font.name = "CoFoRobert-Medium"
font.size = 12.0
# font.size_tolerance = 1e-5
# font.color = 0x333333
# font.superscript = false
# font.italic = false
# font.serif = true
# font.monospace = false
# font.bold = false
# bbox.left = 128.5
# bbox.top = 358.7200012207031
# bbox.right = 208.11978149414062
# bbox.bottom = 373.52801513671875
# bbox.tolerance = 1e-5
[[heading]]
预期输出:
[[heading]]
# Complex Plane
level = 1
greedy = true
font.name = "CoFoRobert-Medium"
font.size = 12.0
# font.size_tolerance = 1e-5
# font.color = 0x333333
# font.superscript = false
# font.italic = false
# font.serif = true
# font.monospace = false
# font.bold = false
# bbox.left = 128.5
# bbox.top = 358.7200012207031
# bbox.right = 208.11978149414062
# bbox.bottom = 373.52801513671875
# bbox.tolerance = 1e-5
解决方案
它匹配它找到的 ^font.size = 12.0$ 行的段落
如果你想要整段文本,那么最简单的方法是使用段落模式。要么使用switch -00,要么设置 $/ = ""。这样readline会读取直到双换行符 \n\n+,你就可以直接打印整行。
perl -00 -ne'print if /^\Qfont.size = 12.0\E$/mg;' input.txt
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。