問題描述
某個(gè)表中有多行數(shù)據(jù),其中第一列數(shù)據(jù)是一個(gè)ID編號(hào)博秫,想知道該ID編號(hào)是不是該表的主鍵?
解決思路
wc -l file
awk -F"," '{print $1}' file | sort | uniq | wc -l
比較wc輸出的行數(shù)是否一致
注意事項(xiàng)
- 為什么需要sort呢潦牛?
- 答:uniq的一個(gè)特性,檢查重復(fù)行的時(shí)候台盯,只會(huì)檢查相鄰的行是否有重復(fù)數(shù)據(jù)罢绽,肯定存在重復(fù)數(shù)據(jù)不是在相鄰位置的情況。
- 實(shí)例
[root@localhost ~]# cat uniqtest #測(cè)試文件
this is a test
this is a test
this is a test
i am tank
i love tank
i love tank
this is a test
whom have a try
WhoM have a try
you have a try
i want to abroad
those are good men
we are good men
[zhangy@BlackGhost mytest]$ uniq -c uniqtest #uniq的一個(gè)特性静盅,檢查重復(fù)行的時(shí)候良价,只會(huì)檢查相鄰的行是否有重復(fù)數(shù)據(jù),肯定存在重復(fù)數(shù)據(jù)不是在相鄰位置的情況
3 this is a test
1 i am tank
2 i love tank
1 this is a test #和第一行是重復(fù)的
1 whom have a try
1 WhoM have a try
1 you? have a try
1 i want to abroad
1 those are good men
1 we are good men
[zhangy@BlackGhost mytest]$ sort uniqtest |uniq -c #這樣就可以解決上個(gè)例子中提到的問題
1 WhoM have a try
1 i am tank
2 i love tank
1 i want to abroad
4 this is a test
1 those are good men
1 we are good men
1 whom have a try
1 you have a try