我在这里寻找这个,但没有找到确切的案例。对不起,如果它是重复的,但我找不到它。
我在 Debian 中有一个巨大的文件,其中包含由“#”分隔的 4 列,格式如下:
username#source#date#time
例如:
A222222#Windows#2014-08-18#10:47:16
A222222#Juniper#2014-08-07#14:31:40
A222222#Juniper#2014-08-08#09:15:34
A111111#Juniper#2014-08-10#14:32:55
A111111#Windows#2014-08-08#10:27:30
我想根据前两列打印唯一的行,如果发现重复项,它必须根据日期/时间打印最后一个事件。使用上面的列表,结果应该是:
A222222#Windows#2014-08-18#10:47:16
A222222#Juniper#2014-08-08#09:15:34
A111111#Juniper#2014-08-10#14:32:55
A111111#Windows#2014-08-08#10:27:30
我已经使用两个命令对其进行了测试:
cat file | sort -u -t# -k1,2
cat file | sort -r -u -t# -k1,2
但是他们都打印了以下内容:
A222222#Windows#2014-08-18#10:47:16
A222222#Juniper#2014-08-07#14:31:40 --> Wrong line, it is older than the duplicate one
A111111#Juniper#2014-08-10#14:32:55
A111111#Windows#2014-08-08#10:27:30
有什么办法吗?
谢谢!
最佳答案
这应该可行
tac file | awk -F# '!a[$1,$2]++' | tac
输出
A222222#Windows#2014-08-18#10:47:16
A222222#Juniper#2014-08-08#09:15:34
A111111#Juniper#2014-08-10#14:32:55
A111111#Windows#2014-08-08#10:27:30
关于bash - 如何按列删除重复项(逆序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363994/