我有一个csv文件中的原始和未经过滤的记录(超过1000000条记录),我想从文件列表中筛选出这些记录(每个文件重量超过282MB;约200多万条记录)。我尝试使用在C的strstr这是我的代码:过滤从巨大的.csv文件中的文本,在C
while (!feof(rawfh)) //loop to read records from raw file
{
j=0; //counter
while((c = fgetc(rawfh))!='\n' && !feof(rawfh)) //read a line from raw file
{
line[j] = c; line[j+1] = '\0'; j++;
}
//function to extract the element in the specified column, in the CSV
extractcol(line, relcolraw, entry);
printf("\nWorking on : %s", entry);
found=0;
//read a set of 4000 bytes; this is the target file
while(fgets(buffer, 4000, dncfh)!=NULL && !found)
{
if(strstr(buffer, entry) !=NULL) //compare it
found++;
}
rewind(dncfh); //put the file pointer back to the start
// if the record was not found in the target list, write it into another file
if(!found)
{
fprintf(out, "%s,\n", entry); printf(" *** written to filtered ***");
}
else
{
found=0; printf(" *** Found ***");
}
//I hope this is the right way to null out a string
entry[0] = '\0'; line[0] ='\0';
//just to display a # on the screen, to let the user know that the program
//is still alive and running.
rawreccntr++;
if(rawreccntr>=10)
{
printf("#"); rawreccntr=0;
}
}
此程序需要大约7到10秒,平均,来搜索在目标文件(282 MB)一个条目。所以,10 * 1000000 = 10000000秒:(上帝知道要花多少钱,如果我决定在25个文件中搜索。
我正在考虑编写一个程序,而不是去勺子喂解决方案(grep, sed等)哦,不好意思,但是我用的是Windows 8(64位,4GB内存,AMD处理器Radeon 2核心--1000Mhz),我用DevC++(gcc)来编译这个。
请赐教你的想法。提前
谢谢,对不起,如果我听起来很蠢。
更新由Ali,从评论中提取关键信息:
我与客户的电话号码和详细地址原始CSV文件。我有CSV格式的目标文件;不要呼叫列表。我想编写一个程序来过滤掉电话号码,这些电话号码在Do No Call List中不存在。电话号码(两个文件)都在第二栏。然而,我不知道任何其他方法。我搜索了Boyer-Moore算法,但是无法在C中实现这一点。任何有关我应该如何去搜索记录的建议?
嗨阿里,我想读一行,然后从原始文件中提取一个特定列的条目。一旦我从原始文件和未经过滤的文件中获得条目,我正试图在目标文件中查看该文件,该文件的文件大小超过了2000000.我将文件指针指向开头,以查找原始文件中的下一条记录。 –
@ShineJacob:你没有回答你想要做什么,但你想怎么做。我们已经知道你想以错误的方式做,但仍然徘徊你想做的事情。又如此:你想做什么?不要告诉我们如何。我们看到它,这是错误的。我们需要更多关于这些数据特征的信息。特别是什么字符,什么长度的条目,是否有一些结构等,因为复杂性正在杀死你。我们必须减少它。 –
@ Hynek-Pichi-Vychodil - 我有原始的CSV与客户的姓名,电话号码和地址。我拥有的另一个文件是CSV格式的“不呼叫列表”。 原始文件看起来像这样:托马斯安德森,8821232313,“A-333,我amlost街道。” 这就是目标文件(不要呼叫清单)的样子:“18”,“1835057558”,“0”,“A”,“1”。这里的相关数据在第二栏。 我想过滤掉那些没有出现在Do No Call List中的数字(来自Raw文件)。谢谢:) –