【问题标题】:Comparing two csv files based on multiple columns and save in separate file基于多列比较两个csv文件并保存在单独的文件中
【发布时间】:2013-11-06 22:26:15
【问题描述】:

我有两个格式相同的文件,其中一个有新更新,另一个有旧更新。没有特定的唯一 id 列。

如何仅提取新的更新行(使用 unix、PHP、AWK)?

【问题讨论】:

    标签: php unix sed grep gawk


    【解决方案1】:

    您想“字节”将所有行与其他行进行比较,所以我会这样做:

    $lines1 = file('file1.txt');
    $lines2 = file('file2.txt');
    
    $lookup = array();
    
    foreach($lines1 as $line) {
      $key = crc32($line);
      if (!isset($lookup[$key])) $lookup[$key] = array();
      $lookup[$key][] = $line;
    }
    
    foreach($lines2 as $line) {
      $key = crc32($line);
    
      $found = false;
      if (isset($lookup[$key])) {
        foreach($lookup[$key] as $lookupLine) {
          if (strcmp($lookupLine, $line) == 0) {
            $found = true;
            break;
          }
        }
      }
    
      // check if not found
      if (!$found) {
        // output to file or do something
      }
    }
    

    请注意,如果文件非常大,这将消耗相当多的内存,您需要使用其他机制,但想法保持不变

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2021-04-27
      • 2021-09-27
      相关资源
      最近更新 更多