根據前三個常見列合併記錄,並將不常見的值以逗號分隔放在最後一列。我有要求執行以下操作-
輸入檔 :
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);
?>