如何用BASH中的递增数替换所有匹配?

前端之家收集整理的这篇文章主要介绍了如何用BASH中的递增数替换所有匹配?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的文本文件
AAAAAA this is some content.
This is AAAAAA some more content AAAAAA. AAAAAA
This is yet AAAAAA some more [AAAAAA] content.

我需要用递增的数字替换所有出现的AAAAAA,例如,输出看起来像这样:

1 this is some content.
This is 2 some more content 3. 4
This is yet 5 some more [6] content.

如何用递增的数字替换所有匹配?

这是一种方法
$awk '{for(x=1;x<=NF;x++)if($x~/AAAAAA/){sub(/AAAAAA/,++i)}}1' file
1 this is some content.
This is 2 some more content 3. 4
This is yet 5 some more [6] content.

猜你在找的Bash相关文章