根据前三个常见列合并记录,并将不常见的值以逗号分隔放在最后一列中。我有要求执行以下操作-
输入文件 :
HWF CommonDomain javatools AdminServer
ABC CommonDomain resourcebundle AdminServer
HWF CommonDomain stringeditor AdminServer
ABC CommonDomain resourcebundle HelpPortalServer_1
HWF CommonDomain stringeditor HelpPortalServer_1
HWF CommonDomain javatools HelpPortalServer_1
预期输出:
ABC CommonDomain resourcebundle AdminServer,HelpPortalServer_1
HWF CommonDomain javatools AdminServer, HelpPortalServer_1
HWF CommonDomain stringeditor AdminServer,HelpPortalServer_1
答案1
如果不使用更高级别的脚本语言,您将无法轻松做到这一点。如果我这样做,我会使用 PHP 并做类似的事情
<?php
$infile = fopen("/path/to/input/file.txt", "r");
$outfile = fopen("/path/to/output/file.txt", "w");
$data = array();
while (($buffer = fgets($infile, 4096)) !== false) {
$parts = explode(' ', $buffer);
for($i=1; $i<count($parts); $i++) {
$data[$parts[0]][$parts[$i]=1;
}
}
foreach($data as $key=>$values) {
$buffer = "";
$buffer .= $key.' ';
foreach(array_keys($values) as $v) {
buffer .= $v.' ';
}
$buffer .= "\n";
fwrite($outfile, $buffer);
}
fclose($infile);
fclose($outfile);
?>