Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Text 使用另一个文本文件中的ID筛选大文本文件_Text_Filtering - Fatal编程技术网

Text 使用另一个文本文件中的ID筛选大文本文件

Text 使用另一个文本文件中的ID筛选大文本文件,text,filtering,Text,Filtering,我有一个两个文本文件,一个文件由大约60000行和14列组成,另一个文件有一列,包含第一个文件中一列(第一列)的子集。我想根据文件2中的ID名称筛选文件1。我在网上试过一些命令,但都没有用。这是两个文本文件的几行(我在linux系统上) 文件1: Contig100 orange1.1g013919m 75.31 81 12 2 244 14 2 78 4e-29 117 1126

我有一个两个文本文件,一个文件由大约60000行和14列组成,另一个文件有一列,包含第一个文件中一列(第一列)的子集。我想根据文件2中的ID名称筛选文件1。我在网上试过一些命令,但都没有用。这是两个文本文件的几行(我在linux系统上)

文件1:

Contig100       orange1.1g013919m       75.31   81      12      2       244     14      2       78      4e-29     117   1126    435
Contig1000      orange1.1g045442m       65.50   400     130     2       631     1809    2       400     1e-156    466   2299    425
Contig10005     orange1.1g003445m       83.86   824     110     2       3222    808     1       820     0.0      1322   3583    820
Contig10006     orange1.1g047384m       81.82   22      4       0       396     331     250     271     7e-05   41.6    396     412
文件2:

Contig1
Contig1000
Contig10005
Contig10017
请让我知道你解决这个问题的好建议


提前感谢。

如果您使用的是Linux/Mac,您可以在命令行上执行此操作(
$
表示命令提示符,请勿键入)

首先,我们从您的
file2
创建一个
file2模式,方法是将
*
附加到每一行:

$ while read line; do echo "$line .*"; done < file2 > file2-patterns
现在我们可以使用这些模式从
file1
中筛选出行

$ grep -f file2-patterns file1
Contig1000      orange1.1g045442m       65.50   400     130     2       631     1809    2       400     1e-156    466   2299    425
Contig10005     orange1.1g003445m       83.86   824     110     2       3222    808     1       820     0.0      1322   3583    820

可以使用python执行此操作:

with open('filter.txt', 'r') as f:
    mask = f.read()

with open('data.txt', 'r') as f:
    while True:
        l = f.readline()
        if not l:
            break
        if l.split(' ')[0] in mask:
            print(l[:-1])

谢谢你的及时回复。对不起,我对这个领域很陌生。请您告诉我如何执行您的解决方案好吗?现在,我知道您的解决方案了。但是,我有一个大文件2,你能让我知道正确的命令添加。*到文件2的每一行吗?很抱歉我愚蠢的评论。我没有注意到您的答案的第一行是从文件2创建模式。请接受我深深的歉意,非常感谢你的帮助。我在一台256G内存的服务器上试用你的解决方案。它在运行40分钟后完成,没有产生任何错误或输出。你的专业观点有什么问题?我会先在你上面发布的小文件上试试,这样你会很快注意到哪里出了问题。。。(这对我来说很有用)顺便问一下,你熟悉命令行和
操作符吗?
with open('filter.txt', 'r') as f:
    mask = f.read()

with open('data.txt', 'r') as f:
    while True:
        l = f.readline()
        if not l:
            break
        if l.split(' ')[0] in mask:
            print(l[:-1])