Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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
Algorithm 在两个给定的输入文件中搜索公共字符串_Algorithm_Bash_Shell_Awk - Fatal编程技术网

Algorithm 在两个给定的输入文件中搜索公共字符串

Algorithm 在两个给定的输入文件中搜索公共字符串,algorithm,bash,shell,awk,Algorithm,Bash,Shell,Awk,我有两个大小为20Gb的文件。我必须在它们之间搜索公共字符串。假设字符串的最大长度为20字节。所以为了解决这个问题,我使用了下面的算法,我使用了一个8GB RAM和四核I3CPU的系统 sort the files using any suitable sorting utility open files A and B for reading read wordA from A read wordB from B while (A not EOF and B not EOF) { if

我有两个大小为20Gb的文件。我必须在它们之间搜索公共字符串。假设字符串的最大长度为20字节。所以为了解决这个问题,我使用了下面的算法,我使用了一个8GB RAM和四核I3CPU的系统

sort the files using any suitable sorting utility
open files A and B for reading
read wordA from A
read wordB from B
while (A not EOF and B not EOF)
{
    if (wordA < wordB)
        read wordA from A
    else if (wordA > wordB)
        read wordB from B
    else
        /* match found, store it into some other files */
        write wordA into output
        read wordA from A
}
使用任何合适的排序实用程序对文件进行排序
打开文件A和B进行读取
读一篇文章
从B读单词B
while(A非EOF和B非EOF)
{
if(wordAwordB)
从B读单词B
其他的
/*找到匹配项,将其存储到其他一些文件中*/
将wordA写入输出
读一篇文章
}
它成功地进行了上述系统配置,但当我在一个6Gb RAM、120GB可用磁盘空间、6核i3处理器的系统中运行此算法时。。。我的系统被关闭了很多次。为什么会这样


请告诉我解决这个问题的其他方法!我们可以提高it性能吗?

是的,您可以使用非常短的
awk
1-liner显著提高性能

awk 'FNR==NR{a[$0]++;next}a[$0]' file1 file2
使用
awk
可以找到唯一的行,而无需首先对它们进行排序。你没有说你想用普通线条做什么,所以我只是假设你想把它们打印出来

如果您只想打印一次公共行,无论它重复多少次,您可以使用以下方法:

awk 'FNR==NR{a[$0]=1;next}a[$0]-- > 0' file1 file2

伪代码没有告诉我们关于您的implementation@Duck“SEGFULT?记忆不足?”我不明白它是怎么发生的。实际上,当我用8Gb内存运行这个程序时,它使用了大约1.2GB内存空间和0%的交换空间,所以我认为它也应该用6Gb内存运行。但是它不工作为什么???@MitchWheat是的,确定它没有提到实现,那么我如何用其他一些系统配置运行上面的算法还有其他更好的逻辑可以在任何系统中运行吗系统?@Gopal我想也是@Gopal,哪个部分有问题?这里的搜索更复杂,需要更多的资源。你能简单地对文件进行排序而没有任何问题吗?好的,看起来很好。你能澄清为什么以及它有多好吗?@Gopal我提到过你不需要预排序,所以这里有两次完整的运行,从任务中删除的文件。多好只是一个你能回答的问题。