Php 如何格式化此文本文件

Php 如何格式化此文本文件,php,shell,Php,Shell,我有一个包含如下内容的文本文件 customer-1 Product-2 customer-1 Product-3 customer-1 Product-7 customer-2 Product-20 customer-2 Product-12 ... 我想把它格式化成这样 customer-1 Product-2, Product-3, Product-7, Product-20 customer-2 Product-20, Product-12

我有一个包含如下内容的文本文件

   customer-1 Product-2
   customer-1 Product-3
   customer-1 Product-7
   customer-2 Product-20
   customer-2 Product-12
   ...
我想把它格式化成这样

  customer-1 Product-2, Product-3, Product-7, Product-20
  customer-2 Product-20, Product-12
  ...
如何在shell脚本或php中实现它


谢谢

将文件导入此awk脚本。在转炉中将有一条额外的生产线,如果需要移除,请使用“head”

awk 'BEGIN { one = ""; } { if ( one != $1 ) { printf("\n%s %s",$1,$2); one = $1; } else { printf(" %s",$2); } } END { printf("\n"); }'

通过管道将文件传输到此awk脚本。在转炉中将有一条额外的生产线,如果需要移除,请使用“head”

awk 'BEGIN { one = ""; } { if ( one != $1 ) { printf("\n%s %s",$1,$2); one = $1; } else { printf(" %s",$2); } } END { printf("\n"); }'

以下是您可以将其分解的方式:

  • 加载内容
  • 对于每一行,找到客户和产品
  • 按客户对产品进行分组
  • 输出
例如:

// preg_match_all will find all "customer-" (digits) and "product-" (digits)
if (preg_match_all('/(customer-\d+)\s*(product-\d+)/i', $s, $matches, PREG_SET_ORDER)) {
        $purchases = array();
        foreach ($matches as $match) {
                // $match[1] contains customer id
                // $match[2] contains product id
                $purchases[$match[1]][] = $match[2];
        }
        // $purchases now contains a list of products, grouped by customer
        foreach ($purchases as $customer => $products) {
                echo $customer, ' ', join(', ', $products), PHP_EOL;
        }
}

以下是您可以将其分解的方式:

  • 加载内容
  • 对于每一行,找到客户和产品
  • 按客户对产品进行分组
  • 输出
例如:

// preg_match_all will find all "customer-" (digits) and "product-" (digits)
if (preg_match_all('/(customer-\d+)\s*(product-\d+)/i', $s, $matches, PREG_SET_ORDER)) {
        $purchases = array();
        foreach ($matches as $match) {
                // $match[1] contains customer id
                // $match[2] contains product id
                $purchases[$match[1]][] = $match[2];
        }
        // $purchases now contains a list of products, grouped by customer
        foreach ($purchases as $customer => $products) {
                echo $customer, ' ', join(', ', $products), PHP_EOL;
        }
}
使用PHP:

  • 使用
    file\u get\u contents()
  • explode()
    每个换行符上的结果字符串
    \n
  • 循环遍历生成的数组,并在每个空格上分解()
  • 然后将数据加载到多维数组中,并可以循环以将其转换为所需的格式

    添加了代码示例(未测试):

    $file = file_get_contents('../path/to/file.txt');
    $rows = explode('\n', $file);
    
    $customers = array();
    
    foreach($rows as $row) {
        $rowPieces = explode(' ',$row);
        $customers[$rowPieces[0]][] = $rowPieces[1];
    }
    
    foreach($customers as $c => $products) {
        $customers[$c] = implode(', ',$products);
    }
    
    echo implode('\n', $customers);
    
    使用PHP:

  • 使用
    file\u get\u contents()
  • explode()
    每个换行符上的结果字符串
    \n
  • 循环遍历生成的数组,并在每个空格上分解()
  • 然后将数据加载到多维数组中,并可以循环以将其转换为所需的格式

    添加了代码示例(未测试):

    $file = file_get_contents('../path/to/file.txt');
    $rows = explode('\n', $file);
    
    $customers = array();
    
    foreach($rows as $row) {
        $rowPieces = explode(' ',$row);
        $customers[$rowPieces[0]][] = $rowPieces[1];
    }
    
    foreach($customers as $c => $products) {
        $customers[$c] = implode(', ',$products);
    }
    
    echo implode('\n', $customers);
    

    我在找一条单行线什么的。我还在想我在找一行什么的。仍然在想,如果您的数据可能包含不一致(双空格等)@Jack使用preg_match的解决方案可能更可靠。但这实际上只是给你一个想法,而不是一个完整的解决方案。如果你的数据可能包含不一致(双空格等)@Jack使用preg_match的解决方案可能更可靠。但这实际上只是给你一个想法,而不是一个完整的解决方案。