linux中單個文件統計重復項、去重復、取唯一項、統計重復次數


1、測試數據

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# cat a.txt     ## 測試數據
a
g
b
d
a
b
b
d
c
b

 

2、統計重復項

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# cat a.txt
a
g
b
d
a
b
b
d
c
b
root@ubuntu01:/home/test# sort a.txt | uniq -d   ## 重復項
a
b
d
root@ubuntu01:/home/test# sort a.txt | uniq -D   ## 重復項
a
a
b
b
b
b
d
d

 

3、去重復

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# cat a.txt
a
g
b
d
a
b
b
d
c
b
root@ubuntu01:/home/test# sort -u a.txt  ## 去重復
a
b
c
d
g
root@ubuntu01:/home/test# sort a.txt | uniq    ## 去重復
a
b
c
d
g

 

4、取唯一項

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# cat a.txt
a
g
b
d
a
b
b
d
c
b
root@ubuntu01:/home/test# sort a.txt | uniq -u    ## 取唯一項
c
g

 

5、統計重復次數

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# cat a.txt
a
g
b
d
a
b
b
d
c
b
root@ubuntu01:/home/test# sort a.txt | uniq -c    ## 統計重復次數 2 a
      4 b
      1 c
      2 d
      1 g
root@ubuntu01:/home/test# sort a.txt | uniq -c | sed 's/^[\t ]*//g'    ## 統計重復次數
2 a
4 b
1 c
2 d
1 g

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM