linux中实现去重复且保持原来的顺序

 

001、去重复保持原来的顺序

[root@pc1 test01]# ls
a.txt
[root@pc1 test01]# cat a.txt            ## 测试数据
1
2
5
5
3
3
7
7
4
[root@pc1 test01]# awk 'ay[$0]++' a.txt    ## 输出重复项,且保持原来的顺序
5
3
7
[root@pc1 test01]# awk '!ay[$0]++' a.txt    ## 去除重复项,且保持原来的顺序
1
2
5
3
7
4

 

002、取唯一项,并保持原来的顺序

[root@pc1 test01]# ls
a.txt  test.sh
[root@pc1 test01]# cat a.txt         ## 测试数据
1
2
5
5
3
3
7
7
4
[root@pc1 test01]# cat test.sh       ## 测试脚本
#!/bin/bash

dup=$(awk 'ay[$0]++' a.txt | wc -l)

awk 'ay[$0]++' a.txt | cat - a.txt | awk -v a=$dup '!ay[$0]++ && NR > a'
[root@pc1 test01]# bash test.sh     ## 取唯一项,保持原来的顺序
1
2
4

 。

 

参考:https://www.cnblogs.com/chenwenyan/p/17572197.html 

 

posted @ 2023-09-07 22:59  小鲨鱼2018  阅读(37)  评论(0编辑  收藏  举报