输入:
X Y
1 11
1 12
2 21
2 22
期望的输出:
11 12
21 22
我想按第 1 列的值转置 CSV 文件。在此示例中,对于 X = 1,将 (11, 12)^T 转置为 (11, 12);对于 X = 2,将 (21, 22)^T 转置为 (21, 22)
答案1
珀尔
perl -lane '
push @{$rows{$F[0]}}, $F[1] if $. > 1
} END {
$, = " ";
print @{$rows{$_}} for (sort keys %rows);
' file
awk,假设输入在第 1 列上排序:
awk '
NR == 1 {next}
NR == 2 {key = $1}
$1 != key {print ""; key = $1}
{printf "%s ", $2}
END {print ""}
' file