有没有一种方法可以按列进行"uniq"? [英] Is there a way to 'uniq' by column?

查看:144
本文介绍了有没有一种方法可以按列进行"uniq"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,如下所示:

I have a .csv file like this:

stack2@example.com,2009-11-27 01:05:47.893000000,example.net,127.0.0.1
overflow@example.com,2009-11-27 00:58:29.793000000,example.net,255.255.255.0
overflow@example.com,2009-11-27 00:58:29.646465785,example.net,256.255.255.0
...

我必须从文件中删除重复的电子邮件(整行)(即,上面示例中包含overflow@example.com的行之一).如何仅在字段1(用逗号分隔)上使用uniq?根据manuniq没有列选项.

I have to remove duplicate e-mails (the entire line) from the file (i.e. one of the lines containing overflow@example.com in the above example). How do I use uniq on only field 1 (separated by commas)? According to man, uniq doesn't have options for columns.

我尝试过使用sort | uniq进行操作,但是它不起作用.

I tried something with sort | uniq but it doesn't work.

推荐答案

sort -u -t, -k1,1 file

  • -u代表唯一
  • -t,所以逗号是定界符
  • -k1,1为关键字段1
    • -u for unique
    • -t, so comma is the delimiter
    • -k1,1 for the key field 1
    • 测试结果:

      overflow@domain2.com,2009-11-27 00:58:29.793000000,xx3.net,255.255.255.0 
      stack2@domain.com,2009-11-27 01:05:47.893000000,xx2.net,127.0.0.1 
      

      这篇关于有没有一种方法可以按列进行"uniq"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆