加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:51:09 所属栏目:Linux 来源:网络整理
导读:有没有办法通过命令行工具过滤掉文件中的所有唯一行而不对行进行排序?我想基本上这样做: sort -u myFile 没有排序的性能. 解决方法 删除重复的行: awk '!a[$0]++' file 这是着名的awk单行.对inet有很多解释. Here是一个解释: This one-liner is very idi
有没有办法通过命令行工具过滤掉文件中的所有唯一行而不对行进行排序?我想基本上这样做:

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 }”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读