在 UNIX 中从另一个文件中查找一个文件的内容 [英] Find content of one file from another file in UNIX
问题描述
我有 2 个文件.第一个文件包含数据库中表的元组的行 ID 列表.第二个文件包含在查询的where"子句中使用这些行 ID 的 SQL 查询.
I have 2 files. First file contains the list of row ID's of tuples of a table in the database. And second file contains SQL queries with these row ID's in "where" clause of the query.
例如:
文件 1
1610657303
1610658464
1610659169
1610668135
1610668350
1610670407
1610671066
文件 2
update TABLE_X set ATTRIBUTE_A=87 where ri=1610668350;
update TABLE_X set ATTRIBUTE_A=87 where ri=1610672154;
update TABLE_X set ATTRIBUTE_A=87 where ri=1610668135;
update TABLE_X set ATTRIBUTE_A=87 where ri=1610672153;
我必须读取文件 1 并在文件 2 中搜索与文件 1 中的行 ID 匹配的所有 SQL 命令,并将这些 SQL 查询转储到第三个文件中.
I have to read File 1 and search in File 2 for all the SQL commands which matches the row ID's from File 1 and dump those SQL queries in a third file.
文件 1 有 1,00,000 个条目,文件 2 包含文件 1 条目的 10 倍,即 1,00,0000.
File 1 has 1,00,000 entries and File 2 contains 10 times the entries of File 1 i.e. 1,00,0000.
我使用了 grep -f File_1 File_2 >文件_3
.但这非常慢,速度是每小时 1000 个条目.
I used grep -f File_1 File_2 > File_3
. But this is extremely slow and the rate is 1000 entries per hour.
有没有更快的方法来做到这一点?
Is there any faster way to do this?
推荐答案
awk
的一种方式:
awk -v FS="[ =]" 'NR==FNR{rows[$1]++;next}(substr($NF,1,length($NF)-1) in rows)' File1 File2
这应该很快.在我的机器上,创建 100 万个条目的查找并将其与 300 万行进行比较花费了不到 2 秒的时间.
This should be pretty quick. On my machine, it took under 2 seconds to create a lookup of 1 million entries and compare it against 3 million lines.
机器规格:
Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz (8 cores)
98 GB RAM
这篇关于在 UNIX 中从另一个文件中查找一个文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!