如何把序列号只在重复的行结束 [英] How to put sequential numbers only at end of the repeated lines

查看:135
本文介绍了如何把序列号只在重复的行结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些重复行的文件。我想提出的序列号只在这些重复的结束

I have a file with some repeated lines. I want to put a sequential number only at the end of these repetitions.

例。

输入:

Jose da Silva
Jose da Silva
Fulano de Tal
Jose da Silva
Sicrano Pereira
Ze Ruela
Sicrano Pereira
Jose da Silva

输出:

Jose da Silva #1
Jose da Silva #2
Fulano de Tal
Jose da Silva #3
Sicrano Pereira #1
Ze Ruela
Sicrano Pereira #2
Jose da Silva #4

我不能将这些行进行排序。他们必须出现在同一个数量级。

I cannot sort these lines. They have to appear on the same order.

推荐答案

循环两次:

awk 'FNR==NR {count[$0]++; next} 
     count[$0]>1 {$0=$0 FS "#"++times[$0]}
     1' file file

这就是:第一次,计数每行出现的次数。第二次,保持一个附加递增的数字以那些出现不止一次。

That is: the first time, count how many times each line occurs. The second time, keep appending an incrementing number to those that appear more than once.

$ awk 'FNR==NR {count[$0]++; next} count[$0]>1 {$0=$0 FS "#"++times[$0]}1' file file
Jose da Silva #1
Jose da Silva #2
Fulano de Tal
Jose da Silva #3
Sicrano Pereira #1
Ze Ruela
Sicrano Pereira #2
Jose da Silva #4

这篇关于如何把序列号只在重复的行结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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