linux去重命令

linux去重命令,第1张

linux去重命令是什么呢?

在介绍uniq命令之前,我们先来新建在下面的案例中需要用到的文件/tmp/uniq.txt,内容如下

默认情况下uniq只会检索相邻的重复数据从而去重。在/tmp/uniq.txt中虽然“onmpw web site” 有三条,但是其中一条是和其他两条不相邻的,所以只去重了一条,同理“error php function”也是这种情况。

鉴于以上的检索机制,所以uniq一般情况下要和sort命令一块儿使用。

复制代码

# sort 1.txt | uniq

alpha css web

cat linux command

error php function

hello world

onmpw web site

recruise page site

repeat no data

wello web site

复制代码

现在再看是不是所有的重复项都已经经过去重处理了。

好了,小试牛刀一把以后,下面我们开始对uniq命令的选项进行简单的介绍。

-c 统计每一行数据的重复次数

复制代码

sort 1.txt | uniq -c

1 alpha css web

1 cat linux command

2 error php function

1 hello world

3 onmpw web site

1 recruise page site

1 repeat no data

1 wello web site

复制代码

我们看 “error php function”出现了两次,“onmpw web site”出现了三次。其余的都没有重复项所以为1。

-i 忽略大小写

在1.txt中添加一行数据 “Error PHP function”

复制代码

cat 1.txt

alpha css web

cat linux command

error php function

hello world

onmpw web site

onmpw web site

wello web site

Error PHP function

recruise page site

error php function

repeat no data

onmpw web site

复制代码

复制代码

sort 1.txt | uniq –c

1 alpha css web

1 cat linux command

2 error php function

1 Error PHP function

1 hello world

3 onmpw web site

1 recruise page site

1 repeat no data

1 wello web site

复制代码

我们看结果,uniq默认是区分大小写的。使用-i可以忽略掉大小写问题

复制代码

sort 1.txt | uniq –c –i

1 alpha css web

1 cat linux command

3 error php function

1 hello world

3 onmpw web site

1 recruise page site

1 repeat no data

1 wello web site

复制代码

现在再看是不是大小写已经忽略掉了。

-u 只输出没有重复的数据

复制代码

sort 1.txt | uniq –iu

alpha css web

cat linux command

hello world

recruise page site

repeat no data

wello web site

复制代码

看到没,结果中的“error php function”和“onmpw web site”都没有被输出。

-w N 表示从第一个字符开始只检索N个字符来判重。

复制代码

sort 1.txt | uniq –iw 2

alpha css web

cat linux command

error php function

hello world

onmpw web site

recruise page site

wello web site

复制代码

这里我们让uniq只对前两个字符进行检索,recruit 和 repeat前两个字符都是re,所以这两行也被认为是重复的。

-f N 表示略过前面N个字段,从第N+1个字段开始检索重复数据。以空格符或者tab键为分隔符。

复制代码

sort 1.txt | uniq –icf 2

1 alpha css web

1 cat linux command

3 error php function

1 hello world

4 onmpw web site

1 repeat no data

1 wello web site

复制代码

我们在结果中可以看到,这是略过前面的2个字段,从第三个字段开始判重的。“recruise page site” 和 “onmpw web site”的第三个字段相同,所以被认为是相同的数据。但是我们看到,“wello web site”和“onmpw web site”不但第三个字段相同,第二个也相同。那为什么它不被计入“onmpw web site”的重复数据中呢。对于这个问题就要回到前面说的,uniq只检测相邻的数据是否是重复的。

要解决这个问题还需要在sort命令上着手。还记得sort命令的-k选项吗,没错,我们就用它来解决。

复制代码

sort –k 2 1.txt | uniq –icf 2

1 alpha css web

1 cat linux command

1 repeat no data

1 recruise page site

3 error php function

4 onmpw web site

1 hello world

复制代码

我们看,是不是解决了。

-s N表示略过前面N个字符,关于这个选项的例子我们这里就不再举了,该选项和-f N的用法差不多。只不过-f N是略过前面N个字段;-s是略过前面N个字符。

-d 只输出有重复项的第一条的数据。

sort 1.txt | uniq -idw 2

repeat no data

error php function

onmpw web site

结果只有这三条。为什么会有“repeat no data”这条数据,这里注意-w 2的应用。

-D 对于重复项全部输出

复制代码

sort 1.txt | uniq –iDw 2

repeat no data

recruise page site

error php function

error php function

Error PHP function

onmpw web site

onmpw web site

onmpw web site

复制代码

好了,关于uniq的选项的所有常用的命令已经都介绍完了。关于uniq更详细的信息可以使用命令info uniq。

文件 test.log json文件数据

根据 data.ext.uid 字段排序

使用 jq 去重数据,并只显示 data.ext.uid 字段

多维度去重


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/5935477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-08
下一篇 2023-03-08

发表评论

登录后才能评论

评论列表(0条)

保存