linux – 如何过滤掉文件中的所有唯一行?

前端之家收集整理的这篇文章主要介绍了linux – 如何过滤掉文件中的所有唯一行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有没有办法通过命令行工具过滤掉文件中的所有唯一行而不对行进行排序?我想基本上这样做:

sort -u myFile

没有排序的性能.

最佳答案
删除重复的行:

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

这是着名的awk单行.对inet有很多解释. Here是一个解释:

This one-liner is very idiomatic. It registers the lines seen in the
associative-array “a” (arrays are always associative in Awk) and at
the same time tests if it had seen the line before. If it had seen the
line before,then a[line] > 0 and !a[line] == 0. Any expression that
evaluates to false is a no-op,and any expression that evals to true
is equal to “{ print }”.

原文链接:https://www.f2er.com/linux/440625.html

猜你在找的Linux相关文章