1. 程式人生 > 其它 >linux中單個檔案統計重複項、去重複、取唯一項、統計重複次數

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