2013-02-11 115 views
0

我有两个不同文本组织的文本文件。这两个文件在文本中都包含几个相同的模式(数字)。我想找出哪些模式(数字)出现在这两个文件中,并将它们写入输出文件。如何找到两个文本文件之间的匹配模式并输出到另一个文件?

FILE1.TXT:

blablabla_25947.bkwjcnwelkcnwelckme 

blablabla_111.bkwjcnwelkcnwelckme 

blablabla_65155.bkwjcnwelkcnwelckme 

blablabla_56412.bkwjcnwelkcnwelckme 

FILE2.TXT:

blablabla_647728.bkwjcnwelkcnwelck 
kjwdhcwkejcwmekcjwhemckwejhcmwekch 

blablabla_6387.bkwjcnwelkcnwelckme 
wexkwhenqlciwuehnqweiugfnwekfiugew 
wedhwnejchwenckhwqecmwequhcnkwjehc 
owichjwmelcwqhemclekcelmkjcelkwejc 

blablabla_59148.bkwjcnwelkcnwelckme 
ecmwequhcnkwjehcowichjwmelcwqhemcle 
kcelmkjcelkwejcwecawecwacewwAWWAXEG 

blablabla_111.bkwjcnwelkcnwelckm 
WESETRBRVSSCQEsfdveradassefwaefawecc 

output_file.txt:

111 
+0

这在计算上是一个非常困难的问题,因为你需要匹配“来自每个可能起点的每一个可能的字符串长度”。在上面,你会期望得到多少'b','bl','bla','blab',..等比赛?是否有你想要的“最小匹配长度”?我认为没有内置命令可以完全符合你的想法;如果你自己有一行搜索字符串,这很容易(使用'grep -F file1 file2'),但是你不能......你能做任何事情来更好地解决问题吗? – Floris 2013-02-11 15:44:01

+0

我没有意识到,你只是想匹配数字......这确实使它更容易一些。 – Floris 2013-02-11 16:29:31

回答

1

如何:

$ egrep -o '_[0-9]+\.' file1 | grep -of - file2 | tr -d '_.' 
111 

# Redirect to new file 
$ egrep -o '_[0-9]+\.' file1 | grep -of - file2 | tr -d '_.' > file3 

第一个grep获取所有数字字符串(前面是_和前面的.file1并且此列表用于grep中的匹配file2。使用tr剥离了_.

+1

值得注意的是,通过保持数字周围的分隔符不会出现错误匹配,例如'_1.''不会匹配''_11.'',但'1''会使用grep匹配''11''。 – sotapme 2013-02-11 15:48:31

+0

@sotapme是的,我应该明确地表明这一点。谢谢。 – 2013-02-11 15:49:48

+0

酷,这正是我需要的,非常感谢你! – 2013-02-12 09:10:50

0

我确实试图解决我认为你在冒充的“难题”。以下代码查找file1和file2中找到的最长字符串。如果有多个“最长”字符串,它只会报告找到的第一个字符串。可能会有所帮助的人,在某些时候(虽然可能不是你在这里寻找解决方案):

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/stat.h> 

/* This routine returns the size of the file it is called with. */ 

static unsigned 
get_file_size (const char * file_name) 
{ 
    struct stat sb; 
    if (stat (file_name, & sb) != 0) { 
     fprintf (stderr, "'stat' failed for '%s': %s.\n", 
       file_name, strerror (errno)); 
     exit (EXIT_FAILURE); 
    } 
    return sb.st_size; 
} 

/* This routine reads the entire file into memory. */ 

static unsigned char * 
read_whole_file (const char * file_name) 
{ 
    unsigned s; 
    unsigned char * contents; 
    FILE * f; 
    size_t bytes_read; 
    int status; 

    s = get_file_size (file_name); 
    contents = malloc (s + 1); 
    if (! contents) { 
     fprintf (stderr, "Not enough memory.\n"); 
     exit (EXIT_FAILURE); 
    } 

    f = fopen (file_name, "r"); 
    if (! f) { 
     fprintf (stderr, "Could not open '%s': %s.\n", file_name, 
       strerror (errno)); 
     exit (EXIT_FAILURE); 
    } 
    bytes_read = fread (contents, sizeof (unsigned char), s, f); 
    if (bytes_read != s) { 
     fprintf (stderr, "Short read of '%s': expected %d bytes " 
       "but got %d: %s.\n", file_name, s, bytes_read, 
       strerror (errno)); 
     exit (EXIT_FAILURE); 
    } 
    status = fclose (f); 
    if (status != 0) { 
     fprintf (stderr, "Error closing '%s': %s.\n", file_name, 
       strerror (errno)); 
     exit (EXIT_FAILURE); 
    } 
    return contents; 
} 

int main(int argc, char* argv[]){ 
    int i1, i2, l1, l2, lm; 
    unsigned char longestString[1000]; // lazy way to make big enough. 
    unsigned char tempString[1000]; 
    int longestFound=0; 
    unsigned char *f1, *f2; // buffers with entire file contents 
    f1 = read_whole_file (argv[1]); 
    f2 = read_whole_file (argv[2]); 

    l1 = strlen(f1); 
    l2 = strlen(f2); 

    for(i1 = 0; i1 < l1; i1++) { 
     lm = 0;// length of match 
     for(i2 = i1; i2<l2; i2++) { 
      lm = 0; 

      while (f1[i1+lm] == f2[i2+lm] && (i1+lm<l1) && (i2+lm<l2) && lm < 1000-1) { 
       tempString[lm] = f1[i1+lm]; 
       lm++; 
      } 

      if (lm > longestFound) { 
       tempString[lm]=0; // terminate string 
       strcpy(longestString, tempString); 
       longestFound = lm; 
      } 
     } 

    } 

    printf("longest string found is %d characters:\n", longestFound); 
    printf("%s\n", longestString); 
    free(f1); 
    free(f2); 
    return 0; 
} 

读取整个文件内容的代码被发现在http://www.lemoda.net/c/read-whole-file/index.html

相关问题