1:仅删除内容完全一样重复行
则可以使用sort先进行排序 然后使用 uniq进行删除重复数据即可。必须先使用sort排序。
例如文件 file.txt
sh-3.2# more file.txt
BIT20050106146 6
BIT20050106147 0
BIT20050106147 5
BIT20050106148 3
BIT20050106148 4
BIT20050106149 2
BIT20050106150 1
BIT20050106150 1
BIT20050106150 1
BIT20050106150 1
BIT20050106151 0
sh-3.2# sort file.txt | uniq > target.txt
sh-3.2# more target.txt
BIT20050106146 6
BIT20050106147 0
BIT20050106147 5
BIT20050106148 3
BIT20050106148 4
BIT20050106149 2
BIT20050106150 1
BIT20050106151 0
则删除成功。
2:删除列内容重复的行
例如下例。删除file.txt文件中第一列重复的行
sh-3.2# more file.txt
BIT20050106146 6
BIT20050106147 0
BIT20050106147 5
BIT20050106148 3
BIT20050106148 4
BIT20050106149 2
BIT20050106150 1
BIT20050106150 1
BIT20050106150 1
BIT20050106150 1
BIT20050106151 0
sh-3.2# awk 'a!=$1{a=$1; print}' file.txt
BIT20050106146 6
BIT20050106147 0
BIT20050106148 3
BIT20050106149 2
BIT20050106150 1
BIT20050106151 0
使用
awk 'a!=$1{a=$1; print}' file.txt
awk 'a!=$1{a=$1; print}' file.txt < target.txt
mv target.txt file.txt
则file.txt变为上述的结果。
3: 删除第一个字段重复且第二个字段为0的行。
sh-3.2# more file.txt
BIT20050106146 6
BIT20050106147 0
BIT20050106147 5
BIT20050106148 3
BIT20050106148 4
BIT20050106149 2
BIT20050106150 1
BIT20050106151 0
sh-3.2# awk 'a!=$1{a=$1; print}' file.txt > target.txt
sh-3.2# mv target.txt file.txt
sh-3.2# more file.txt
BIT20050106146 6
BIT20050106147 0
BIT20050106148 3
BIT20050106149 2
BIT20050106150 1
BIT20050106151 0
sh-3.2# awk '$2!=0{print}' file.txt > target.txt
sh-3.2# mv target.txt file.txt
sh-3.2# more file.txt
BIT20050106146 6
BIT20050106148 3
BIT20050106149 2
BIT20050106150 1
可分为两步来完成:
awk 'a!=$1{a=$1; print}' file.txt > target.txt
之后:mv target.txt file.txt
即可。awk '$2!=0{print}' file.txt > target.txt mv target.txt file.txt