如何格式化此文本文件

时间:2012-06-07 02:06:32

标签: 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
  ...

如何在shell脚本或php中执行此操作?

谢谢

3 个答案:

答案 0 :(得分:2)

将您的文件传输到此awk脚本。在BOF中会有一个额外的行,如果你需要删除它,请使用'head'。

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

答案 1 :(得分:2)

使用PHP:

  1. 使用file_get_contents()
  2. 获取文本文件的内容
  3. explode()每个换行符\n
  4. 上的结果字符串
  5. 遍历生成的数组,并在每个空格explode()
  6. 上循环

    然后,您可以将数据加载到多维数组中,并可以循环访问以获得所需的格式。

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

    $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);
    

答案 2 :(得分:1)

这就是你如何分解它:

  • 加载内容
  • 为每一行,找到客户和产品
  • 按客户分组产品
  • 输出

例如:

// 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;
        }
}