如何用awk重命名重复的行?

时间:2015-04-30 20:47:56

标签: awk duplicates rename

我有一个包含100万行的文件,有些行是重复的。我想通过附加" variant"来重命名重复的行。 +一个数字。 该文件的格式如下:

I am a test line
She is beautiful
need for speed
Nice day today
I am a test line
stack overflow is fun
I am a test line
stack overflow is fun
I have more sentences
I am a test line
She is beautiful
Speed for need
stack overflow is fun
Let's stop here

期望的结果:

    I am a test line
    She is beautiful
    need for speed
    Nice day today
    I am a test line variant 1
    stack overflow is fun
    I am a test line variant 2
    stack overflow is fun variant 1
    I have more sentences
    I am a test line variant 3
    She is beautiful variant 1
    Speed for need variant 1
    stack overflow is fun variant 2
    Let's stop here

1 个答案:

答案 0 :(得分:4)

$ awk 'cnt[$0]++{$0=$0" variant "cnt[$0]-1} 1' file
I am a test line
She is beautiful
need for speed
Nice day today
I am a test line variant 1
stack overflow is fun
I am a test line variant 2
stack overflow is fun variant 1
I have more sentences
I am a test line variant 3
She is beautiful variant 1
Speed for need
stack overflow is fun variant 2
Let's stop here