我有一个包含这样内容的文本文件
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中执行此操作?
谢谢
答案 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:
file_get_contents()
explode()
每个换行符\n
explode()
醇>
然后,您可以将数据加载到多维数组中,并可以循环访问以获得所需的格式。
添加了代码示例(未经测试):
$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;
}
}