linux下重复行的删除

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
即可。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值