foo bar, foo bar, foo bar
foo bar
有了这首美丽的诗,怎么能全部bar
替换成superman
,但是每次都有一个不同的结尾(包含在数组中)(is super, is blazing fast, is marvelous, cawabanga)
因此结果将是:
foo superman is super, foo superman is blazing fast, foo superman is marvelous
foo superman cawabanga
我知道可以通过sed使用for / loop来这样做。但是,有没有更短,更漂亮的解决方案?
您可以使用shell参数扩展来做到这一点:这实际上是一种单行代码
poem='foo bar, foo bar, foo bar
foo bar'
traits=( "is super" "is blazing fast" "is marvelous" "cawabanga" )
for trait in "${traits[@]}"; do poem=${poem/bar/"superman $trait"}; done
echo "$poem"
foo superman is super, foo superman is blazing fast, foo superman is marvelous
foo superman cawabanga
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句