比较两个文件,​​并删除任何行文件2,当他们匹配文件1找到的值 [英] Compare 2 files and remove any lines in file2 when they match values found in file1

查看:145
本文介绍了比较两个文件,​​并删除任何行文件2,当他们匹配文件1找到的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件。我试图删除任何行文件2,当他们匹配文件1中找到的值。一个文件有房源,像这样:

I have two files. i am trying to remove any lines in file2 when they match values found in file1. One file has a listing like so:

文件1

ZNI008
ZNI009
ZNI010
ZNI011
ZNI012

...超过19463行

... over 19463 lines

第二个文件包括匹配列在第一位的项目,线:
文件2

The second file includes lines that match the items listed in first: File2

copy /Y \\server\foldername\version\20050001_ZNI008_162635.xml \\server\foldername\version\folder\
copy /Y \\server\foldername\version\20050001_ZNI010_162635.xml \\server\foldername\version\folder\
copy /Y \\server\foldername\version\20050001_ZNI012_162635.xml \\server\foldername\version\folder\
copy /Y \\server\foldername\version\20050001_ZNI009_162635.xml \\server\foldername\version\folder\

...继续上市,直到行51360

... continues listing until line 51360

我试过到目前为止:

grep -v -i -f file1.txt file2.txt > f3.txt

不产生任何输出到 f3.txt 或删除任何行。我通过运行验证

does not produce any output to f3.txt or remove any lines. I verified by running

wc -l file2.txt

和结果

51360 file2.txt

我相信原因是,目前还没有确切的匹配。当我运行下面就显示什么。

I believe the reason is that there are no exact matches. When I run the following it shows nothing

comm -1 -2 file1.txt file2.txt

运行

( tr '\0' '\n' < file1.txt; tr '\0' '\n' < file2.txt ) | sort | uniq -c | egrep -v '^ +1'

只显示一个比赛,尽管我可以清楚地看到有不止一个匹配。

shows only one match, even though I can clearly see there is more than one match.

或者把所有的数据到一个文件并运行以下内容:

Alternatively putting all the data into one file and running the following:

grep -Ev "$(cat file1.txt)" 1>LinesRemoved.log

说的说法有太多的线来处理。

says argument has too many lines to process.

我需要删除的文件2匹配文件1项线。

I need to remove lines matching the items in file1 from file2.

我也是蟒蛇试图这样的:
 `

i am also trying this in python: `

    #!/usr/bin/python
s = set()

# load each line of file1 into memory as elements of a set, 's'
f1 = open("file1.txt", "r")
for line in f1:
    s.add(line.strip())
f1.close()

# open file2 and split each line on "_" separator,
# second field contains the value ZNIxxx
f2 = open("file2.txt", "r")
for line in f2:
    if line[0:4] == "copy":
        fields = line.split("_")
        # check if the field exists in the set 's'
        if fields[1] not in s:
            match = line
        else:
            match = 0
    else:
        if match:
            print match, line,

`

它不工作以及..作为即时得到
回溯(最近通话最后一个):
  文件./test.py,14号线,在?
    如果领域[1]不属于S:
IndexError:列表索引超出范围

it is not working well.. as im getting 'Traceback (most recent call last): File "./test.py", line 14, in ? if fields[1] not in s: IndexError: list index out of range'

推荐答案

什么:

grep -F -v -f file1 file2 > file3

这篇关于比较两个文件,​​并删除任何行文件2,当他们匹配文件1找到的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆