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

use*_*825 4 awk grep sed

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

输入示例: Foo was the +first+ to get a drink at +the bar+.

期望的输出: Foo was the _____ to get a drink at ___ ___.

使用任何标准的shell工具,最简单的(在眼睛上)解决方案是什么?

Ken*_*ent 5

这个awk单行应该可以帮助你:

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

测试

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 ___ ___.
Run Code Online (Sandbox Code Playgroud)