使用php基于第一列合并2个文本文件


Merge 2 text files based on first column using php

我有两个日志文件,有多行,比如

第一个:

1|2016-04-13|...
3|2016-03-13|...

第二:

2|POST|accept: txt|...
3|POST|accept: txt|...

预期结果:

3|2016-03-13|...|POST|accept: txt|...

因此,我需要使用PHP脚本,根据第一列(ID)将所有数据组合在一个文件中。
注意:行数可以不同。只需要交叉点(顺序敏感)

打开这两个日志文件。

您可以使用fopen和fgets(在foreach/while循环中)将行放入数组

或使用file_get_contents通过(''r''n在Win上)分解文件

现在您应该有两个数组,其中包含两个日志文件的行。然后你这样做:

 $log1Lines = array("3|...|...", "4|...|...");
 $log2Lines = array("2|...|...", "3|...|...");
 $merged = array();
foreach($log1Lines as $row1){
     $id1 = explode("|", $row1)[0];
     foreach($log2Lines as $row2){
          $exploded = explode("|", $row2);
          $id2 = array_shift($exploded);
          if($id1 == $id2){
                $merged[$id1] = $row1 . "|" . implode("|", $exploded);
          }
     }
 }
 print_r($merged);

通常,它应该在没有循环的情况下是可行的(通过array_incross比较两个数组之间解析的索引),但我现在还没有这样做的解决方案。

希望能有所帮助。

我最近需要写一些非常类似的东西,所以我为您的格式更新了一些。如果需要,这将支持2个以上的文件,并允许更改分隔符。

<?php
class Merger
{
    protected $separator = '|';
    protected $data = [];
    protected $initialised = false;
    public function mergeFile($filename)
    {
        $file = new SplFileObject($filename);
        $fileKeys = [];
        // Read the information out of the current file
        while (!$file->eof()) {
            $line = $file->fgets();
            $parts = explode($this->separator, trim($line));
            $id = array_shift($parts);
            $fileKeys[] = $id;
            $fileData[$id] = $parts;
        }
        // First pass: add everything
        if (!$this->initialised)
        {
            $this->data = $fileData;
        }
        // Subsequent passes, only add things that have already been seen, then
        // clear out anything that wasn't in the current file
        else
        {
            foreach ($fileData as $id => $data)
            {
                if ($this->data[$id])
                {
                    $this->data[$id] = array_merge($this->data[$id], $data);
                }
            }
            $this->data = array_filter($this->data, function ($e) use ($fileKeys) {
                return in_array($e, $fileKeys);
            }, ARRAY_FILTER_USE_KEY);
        }
        $this->initialised = true;
    }
    public function output($filename)
    {
        foreach ($this->data as $id => $data)
        {
            $output .= $id . $this->separator . implode($this->separator, $data) . PHP_EOL;
        }
        file_put_contents($filename, $output);
    }
}
$merger = new Merger;
$merger->mergeFile('1.txt');
$merger->mergeFile('2.txt');
echo $merger->output('output.txt');

我的解决方案是:

<?php 
    exec ("awk -F'|' -vOFS='|' '(NR==FNR){a[$1]=$0; next}{if(a[$1]){print $2,a[$1]}}' first.log second.log > result.log");
?>

我使用execphp函数来执行shell脚本

awk -F'|' -vOFS='|' '(NR==FNR){a[$1]=$0; next}{if(a[$1]){print $2,a[$1]}}' first.log second.log > result.log

这里-F'|'指定"|"符号作为分隔符,first.logsecond.log是我要合并的文件。