如何用BASH中的递增数字替换所有匹配项? [英] How to replace all matches with an incrementing number in BASH?

查看:81
本文介绍了如何用BASH中的递增数字替换所有匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的文本文件:

I have a text file like this:

AAAAAA this is some content.
This is AAAAAA some more content AAAAAA. AAAAAA
This is yet AAAAAA some more [AAAAAA] content.

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

I need to replace all occurrence of AAAAAA with an incremented number, e.g., the output would look like this:

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

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

How can I replace all of the matches with an incrementing number?

推荐答案

这是一种实现方法:

$ 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中的递增数字替换所有匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆