0

Input:

X Y
1 11
1 12
2 21
2 22

Desired Output:

11 12
21 22

I want to transpose the cvs file by the value of column 1. In this example, for X = 1, transpose (11, 12)^T to (11, 12); for X = 2, transpose (21, 22)^T to (21, 22)

1

perl

perl -lane '
    push @{$rows{$F[0]}}, $F[1] if $. > 1
  } END {
    $, = " ";
    print @{$rows{$_}} for (sort keys %rows);
' file

awk, assumes input is sorted on column 1:

awk '
    NR == 1 {next}
    NR == 2 {key = $1} 
    $1 != key {print ""; key = $1} 
    {printf "%s ", $2} 
    END {print ""}
' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.