我有一个如下所示的文件..
abc, 12345
def, text and nos
ghi, something else
jkl, words and numbers
abc, 56345
def, text and nos
ghi, something else
jkl, words and numbers
abc, 15475
def, text and nos
ghi, something else
jkl, words and numbers
abc, 123345
def, text and nos
ghi, something else
jkl, words and numbers
我想将其转换(加入)为:
abc, 12345, 56345, 15475, 123345
def, text and nos, text and nos,text and nos,text and nos
ghi, something else, something else, something else, something else
jkl, words and numbers, words and numbers, words and numbers, words and numbers
答案1
如果您不介意输出顺序:
$ awk -F',' 'NF>1{a[$1] = a[$1]","$2};END{for(i in a)print i""a[i]}' file
jkl, words and numbers, words and numbers, words and numbers, words and numbers
abc, 12345, 56345, 15475, 123345
ghi, something else, something else, something else, something else
def, text and nos, text and nos, text and nos, text and nos
解释
NF>1
这意味着我们只需要处理非空白行。- 我们将所有第一个字段保存在关联数组中
a
,键是第一个字段,值是第二个字段(或该行的其余部分)。如果键已经有值,我们将连接两个值。 - 在
END
块中,我们循环遍历关联数组a
,打印其所有键和相应的值。
或者使用perl
将保持顺序:
$perl -F',' -anle 'next if /^$/;$h{$F[0]} = $h{$F[0]}.", ".$F[1];
END{print $_,$h{$_},"\n" for sort keys %h}' file
abc, 12345, 56345, 15475, 123345
def, text and nos, text and nos, text and nos, text and nos
ghi, something else, something else, something else, something else
jkl, words and numbers, words and numbers, words and numbers, words and numbers
答案2
噢,这很简单。这是一个简单的版本,它保持键在文件中出现的顺序:
$ awk -F, '
/.+/{
if (!($1 in Val)) { Key[++i] = $1; }
Val[$1] = Val[$1] "," $2;
}
END{
for (j = 1; j <= i; j++) {
printf("%s %s\n%s", Key[j], Val[Key[j]], (j == i) ? "" : "\n");
}
}' file.txt
输出应如下所示:
abc, 12345, 56345, 15475, 123345
def, text and nos, text and nos, text and nos, text and nos
ghi, something else, something else, something else, something else
jkl, words and numbers, words and numbers, words and numbers, words and numbers
如果您不介意末尾有一个额外的空行,只需将该printf
行替换为printf("%s %s\n\n", Key[j], Val[Key[j]]);