比较 2 个文件并在它们匹配 file1 中找到的值时删除 file2 中的任何行 [英] Compare 2 files and remove any lines in file2 when they match values found in file1
问题描述
我有两个文件.当文件 1 中找到的值匹配时,我试图删除文件 2 中的任何行.一个文件有这样的列表:
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 \serverfoldernameversion20050001_ZNI008_162635.xml \serverfoldernameversionfolder
copy /Y \serverfoldernameversion20050001_ZNI010_162635.xml \serverfoldernameversionfolder
copy /Y \serverfoldernameversion20050001_ZNI012_162635.xml \serverfoldernameversionfolder
copy /Y \serverfoldernameversion20050001_ZNI009_162635.xml \serverfoldernameversionfolder
... 继续列出直到第 51360 行
... continues listing until line 51360
到目前为止我尝试过的:
What I've tried so far:
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 ' ' '
' < file1.txt; tr ' ' '
' < 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.
我也在 python 中尝试这个:`
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 中:索引错误:列表索引超出范围'
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 个文件并在它们匹配 file1 中找到的值时删除 file2 中的任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!