【问题标题】:Remove lines that have duplicate entry in the first column删除第一列中有重复条目的行
【发布时间】:2014-04-15 09:12:56
【问题描述】:

我想通过删除每行第一列中的重复条目来过滤我的文件(制表符分隔)。

我试过了:

cut -f1 filename.txt | sort | uniq -u > filename_filtered.txt

但这只会打印出文件的第一列,是否有过滤第一列但打印出整个过滤后的文件?

【问题讨论】:

    标签: shell csv awk filter duplicates


    【解决方案1】:

    这应该可以:

    awk '!a[$1]++' file
    

    它跟踪字段,当第一个字段还没有出现时打印一行。

    测试

    $ cat a
    test    hello   bye
    test    bye     hello
    another thing   here
    how     how     how
    another blab    bla
    text    text    text
    
    $ awk '!a[$1]++' a
    test    hello   bye
    another thing   here
    how     how     how
    text    text    text
    

    【讨论】:

    • 很高兴阅读 :) 如果你完成了,请考虑接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2023-01-11
    • 1970-01-01
    • 2020-09-17
    • 2019-11-14
    相关资源
    最近更新 更多