awk - 如何使用 awk 重命名重复行?

标签 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

最佳答案

$ 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

关于awk - 如何使用 awk 重命名重复行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29977511/

相关文章:

R提取字符串中的重复单词

mysql - 如何更新同一个表上具有不同值的多个重复项?

git - 如何安全地更改github账户名?

java - 如何在 Java 中重命名文件?

python - 在数据框中添加一列,对现有列的不同行执行不同的操作

r - 重命名 data.table 的问题

python - awk:使用多个分隔符时保留原始字段分隔符

regex - 用于合并具有匹配的第一个字段的行的命令行,50 GB 输入

shell - 如何编写一个 shell 脚本来获取特定列的第一条记录?

unix - 单行输出