问题描述
某个表中有多行数据,其中第一列数据是一个ID编号,想知道该ID编号是不是该表的主键?
解决思路
wc -l file
awk -F"," '{print $1}' file | sort | uniq | wc -l
比较wc输出的行数是否一致
注意事项
- 为什么需要sort呢?
- 答:uniq的一个特性,检查重复行的时候,只会检查相邻的行是否有重复数据,肯定存在重复数据不是在相邻位置的情况。
- 实例
[root@localhost ~]# cat uniqtest #测试文件
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的一个特性,检查重复行的时候,只会检查相邻的行是否有重复数据,肯定存在重复数据不是在相邻位置的情况
3 this is a test
1 i am tank
2 i love tank
1 this is a test #和第一行是重复的
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 #这样就可以解决上个例子中提到的问题
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
网友评论