2

两个文件求差集

 2 years ago
source link: https://segmentfault.com/a/1190000040955272
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在平常的做业务需求时会遇到这样一种情况,有两份文件A和B,存的是用户账号,需要在A文件中剔除B文件中已存在的账号,这种情况下应该如何处理呢?

一般这种情况下会选择采用脚本语言处理,比如shell或者python。接下来介绍两种语言的解决方法:

aaa
bbb
ccc
111
222

bbb
ccc
111

1.采用awk

$awk 'NR==FNR{ a[$1]=$1 } NR>FNR{ if(a[$1] == ""){ print $1}}' B A 

2.采用python

#!/usr/bin/python
# -*- coding: utf-8 -*- 

import os

src = []
screen_data = []
dest_data = []

#原数据
for line in open("src.txt"):
    src.append(line.strip())

#筛选名单
for line in open("screen_data.txt"):
    screen_data.append(line.strip())

#目标数据    
for data in src:
    if data in screen_data:
        print data
        
print len(dest_data)

#导出到文件
with open("dest_data.txt","w") as f:
    for data in dest_data:
        f.write(data + '\n')

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK