bash - 如何按列删除重复项(逆序)

标签 bash sorting sed

我在这里寻找这个,但没有找到确切的案例。对不起,如果它是重复的,但我找不到它。

我在 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/

相关文章:

linux - 你能解释一下这个 sed 单行代码吗?

bash 忽略特定错误代码(pytest no tests 警告错误代码5)

java - 如何生成快速排序算法的最坏情况?

vim - 在 Vim 中用恒等运算符 (===) 替换相等运算符 (==)

c++ - 在 cpp 中使用带有 std::vector 的字符串长度对字符串数组进行排序

Java 使用归并排序对数字数组进行排序

json - 用文件内容替换关键字

linux - 命令在脚本中不起作用

bash - 通过SSH在远程Linux服务器上运行命令时,用BASH代替CSH

linux - 使用 bash 在 Linux 中的一堆目录上运行 mvn clean install