linux - 删除几乎没有差异的重复行

标签 linux shell unix text centos

我有一个动态 文本文件,可以自动写入一些行。 但是重复的条目有问题:

例如:

1111 2222 3333 4444 <- I want this line
5555 6666 7777 8888 <- And this line too
1111 2222 3333 4444
5555 6666 7777 9999 <- Note : 9999 is only one ward change

预期结果:

1111 2222 3333 4444
5555 6666 7777 8888

真实测试示例:

exten => 01272786170,1,Set(CALLERID(num)=821)
    same => n,Dial(SIP/port21/01272786170,60,rt)
    same => n,Set(thereis=yes01272786170)
    same => n,Set(calledid=01272786170)
    same => n,GotoIf("calledid" = "01272786170"?ejoin,01272786170,1)
exten => 01272786170,1,Set(CALLERID(num)=826) <- duplicated here with one number change
    same => n,Dial(SIP/port26/01272786170,60,rt) <-
exten => 01272786170,1,Set(CALLERID(num)=827) <-
    same => n,Dial(SIP/port27/01272786170,60,rt) <-

预期结果:

exten => 01272786170,1,Set(CALLERID(num)=821)
    same => n,Dial(SIP/port21/01272786170,60,rt)
    same => n,Set(thereis=yes01272786170)
    same => n,Set(calledid=01272786170)
    same => n,GotoIf("calledid" = "01272786170"?ejoin,01272786170,1)

注意:我希望使用 Linux Shell 完成它。

非常感谢。

最佳答案

使用 awk 和您的第一个示例数据:

如果您实现 Levenshtein 算法(例如 here)并想出足够的编辑距离(下面的 4),您可以使用这种简单的方法:

awk '
function levdist(str1, str2 ...)  # see the above link for working implementation
{
    ...
}
{
    for(i in a) {                 # iterate all previous stored strings
        l=levdist($0,a[i])        # compute the edit distance
        if(l<=4)                  # if below threshold
            next                  # skip to next string 
    }
    print $0                      # output where threshold was not met
    a[NR]=$0                      # store
}' file

输出:

1111 2222 3333 4444
5555 6666 7777 8888

关于linux - 删除几乎没有差异的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51842004/

相关文章:

C++ Linux (Ubuntu) 正确写入串口 (Arduino)

linux - jbd2/sda2-8 的高 io 使用率

bash - 如何将多个命令通过管道传输到 shell 中的单个命令? (嘘,庆典,...)

c++ - 父进程和子进程中的全局变量

linux - C 私钥中的 RSA 算法

linux - 如何监控和记录 mt apache 网络服务器的 session

linux - 如何使用shell脚本将xlsx文件转换为csv文件?

bash - 我可以在从脚本运行的 makefile 中分配系统变量吗?

c - 如何将管道中前一个程序的输出正确发送到下一个程序?

c - AIX 中的 memmove_overlay 命令是什么?