gpt4 book ai didi

awk - 如何非贪婪地替换一行的多个分隔字符串?

转载 作者:行者123 更新时间:2023-12-05 00:18:59 25 4
gpt4 key购买 nike

我想从相应格式化的输入字符串中生成谜语。

示例输入:Foo was the +first+ to get a drink at +the bar+.
期望的输出:Foo was the _____ to get a drink at ___ ___.
使用任何标准的 shell 工具,最简单的(在眼睛上)解决方案是什么?

最佳答案

这个 awk one-liner 应该可以帮助你:

awk -F'+' -v OFS="" 'NF>2{for(i=2;i<=NF;i+=2)gsub(/\S/,"_",$i)}7'

测试
kent$  awk -F'+' -v OFS="" 'NF>2{for(i=2;i<=NF;i+=2)gsub(/\S/,"_",$i)}7' <<<"Foo was the +first+ to get a drink at +the bar+."
Foo was the _____ to get a drink at ___ ___.

关于awk - 如何非贪婪地替换一行的多个分隔字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923290/

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