unqiq 命令通過消除重復(fù)內(nèi)容每聪,從給定的輸入中找出單一的行。它也可以用來找出輸入中出現(xiàn)的重復(fù)行疲眷。uniq只能用于排過序的數(shù)據(jù)輸入禾蚕,因此,uniq要么使用管道狂丝,要私將排過序的文件作為輸入换淆,并總是以這種方式與sort命令結(jié)合起來。
cat sorted.txt
#bash
#foss
#hack
#hack
cat data.txt
#u:01:gnu
#u:04:linux
#u:01:hack
#u:01:hack
例子
1.顯示唯一行
uniq sorted.txt
- 只顯示唯一行
uniq -u sorted.txt
- 統(tǒng)計各行在文件中出現(xiàn)的次數(shù)
uniq -c sorted.txt
- 找出文件的重復(fù)行
uniq -d sorted.txt
- 忽略并指定字符數(shù)來uniq操作
sort data.txt | uniq -s 2 -w 2
- 計算字符串中出現(xiàn)的次數(shù)
echo `echo 'ahebhaa' | sed 's/[^\n]/&\n/g' | sed '/^$/d' | sort | uniq -c | tr -d ' \n'`