GNU awk: accessing captured groups in replacement text

非 Y 不嫁゛ 提交于 2019-11-29 01:02:22
echo abbc | awk '{ print gensub(/a(b*)c/, "Here are bees: \\1", "g", $1);}'

See manual here to see the difference between gsub and gensub

Per the gawk manual

gensub provides an additional feature that is not available in sub or gsub: the ability to specify components of a regexp in the replacement text. This is done by using parentheses in the regexp to mark the components and then specifying ‘\N’ in the replacement text, where N is a digit from 1 to 9.

You must use gensub, you must specify "g", and you must grab the result of gensub, since it does not modify in-place.

awk '{ r = gensub(/a(b*)c/, "Here are bees: \\1", "g"); print r; }'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!