如果字段重复,则删除行 [英] Remove line if field is duplicate

查看:48
本文介绍了如果字段重复,则删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果第一个字段重复,则查找awk(或sed)单行代码以从输出中删除行.

Looking for an awk (or sed) one-liner to remove lines from the output if the first field is a duplicate.

删除重复行的示例如下:

An example for removing duplicate lines I've seen is:

awk 'a !~ $0; {a=$0}'

尝试将其用作没有运气的基础(我认为将$ 0更改为$ 1可以解决问题,但似乎没有效果).

Tried using it for a basis with no luck (I thought changing the $0's to $1's would do the trick, but didn't seem to work).

推荐答案

awk '{ if (a[$1]++ == 0) print $0; }' "$@"

这是关联数组的一种标准用法(非常简单).

This is a standard (very simple) use for associative arrays.

这篇关于如果字段重复,则删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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