bash - 在 bash 中只保留第一个重复条目

标签 bash awk

这是我的数据结构:

First   A   1385
First   B   8364
First   C   9734
First   C   9625
Second  A   3566
Second  B   9625
Second  B   0238

我如何删除重复的行条目(第 1 列和第 2 列中的信息)并保留它的第一次出现。
我想删除:First C 9625Second B 0238 因为它们是 First CSecond B 的第二次出现>,对于这样的结果:

First   A   1385
First   B   8364
First   C   9734
Second  A   3566
Second  B   9625

我尝试过什么:

awk '{print $1"\t"$2}' FILE  | 
   sort -u | 
   while read LINE; do 
      echo $LINE | 
      tr ' ' '\t' | 
      grep -m1 -F -f - FILE
   done

我刚刚学习 bash 编码,我的解决方案非常笨拙。我相信可以用一个 bash 命令做我想做的事情。

最佳答案

$ awk '!seen[$1,$2]++' file
First   A   1385
First   B   8364
First   C   9734
Second  A   3566
Second  B   9625

这就是为什么您需要在字段之间添加 , 的原因:

$ cat file
ab c
a  bc
$
$ awk '!seen[$1,$2]++' file
ab c
a  bc
$ awk '!seen[$1$2]++' file
ab c

关于bash - 在 bash 中只保留第一个重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24217501/

相关文章:

linux - 如何仅从文本文件的第一行和最后一行中提取数据

linux - 从 Linux 命令行处理复杂的 CSV

unix - 删除文本行 block

linux - 为表中的特定列生成随机数

bash - 在 vim 命令行模式下 : how to kill the line from the current cursor position to the end

bash - 使用转义序列呈现文本(如终端)

linux - Bash 中的进程池

regex - 使用awk计算并打印正则表达式得到的值的平均值

bash 羊群 : Why 200?

linux - 使用egrep -o后如何进行egrep