I have a dataset similar to below:
37 151 36 34 40 56 59 42 28 38 60
1 11 0 0 2 2 3 4 0 0 4
35 158 35 37 40 56 58 48 31 40 72
1 2 1 1 0 0 0 0 1 0 0
32 132 32 30 36 57 53 35 25 34 54
8 36 4 8 8 7 13 13 3 6 14
40 162 36 38 41 66 64 46 27 35 60
0 2 0 0 1 0 0 0 1 1 2
32 151 31 34 41 58 66 45 33 40 66
0 5 3 2 1 2 0 1 1 4 6
I want to transform the even rows to even columns like this:
37 1 151 11 36 0 34 0 40 2 56 2 59 3 42 4 28 0 38 0 60 4
35 1 158 2 35 1 37 1 40 0 56 0 58 0 48 0 31 1 40 0 72 0
32 8 132 36 32 4 30 8 36 8 57 7 53 13 35 13 25 3 34 6 54 14
40 0 162 2 36 0 38 0 41 1 66 0 64 0 46 0 27 1 35 1 60 2
32 0 151 5 31 3 34 2 41 1 58 2 66 0 45 1 33 1 40 4 66 6
Basically, I would like to take the even rows and transform them as even columns while retaining the odd rows and odd columns the way they are.
CodePudding user response:
You can do it in a straight-forward manner with awk
by saving each field of the the odd number rows in an array and then printing each array element in between the elements of each even number rows, e.g.
awk '
FNR%2{for(i=1;i<=NF;i )a[i]=$i; next}
{for(i=1;i<=NF;i ) printf " = =", a[i],$i; print ""}
' file
Where the modulo of the record number (line) FNR
is taken to determine odd/even and where odd, the fields are stored in the a[]
array and then the next line (even) is printed where each field is printed after the corresponding field in a[]
.
Example Use/Output
You can adjust the output spacing as wanted. With your data in file
you would get:
$ awk '
> FNR%2{for(i=1;i<=NF;i )a[i]=$i; next}
> {for(i=1;i<=NF;i ) printf " = =", a[i],$i; print ""}
> ' file
37 1 151 11 36 0 34 0 40 2 56 2 59 3 42 4 28 0 38 0 60 4
35 1 158 2 35 1 37 1 40 0 56 0 58 0 48 0 31 1 40 0 72 0
32 8 132 36 32 4 30 8 36 8 57 7 53 13 35 13 25 3 34 6 54 14
40 0 162 2 36 0 38 0 41 1 66 0 64 0 46 0 27 1 35 1 60 2
32 0 151 5 31 3 34 2 41 1 58 2 66 0 45 1 33 1 40 4 66 6
Without Even Output Spacing:
$ awk '
FNR%2{for(i=1;i<=NF;i )a[i]=$i; next}
{for(i=1;i<=NF;i ) printf " %d %d", a[i],$i; print ""}
' file
37 1 151 11 36 0 34 0 40 2 56 2 59 3 42 4 28 0 38 0 60 4
35 1 158 2 35 1 37 1 40 0 56 0 58 0 48 0 31 1 40 0 72 0
32 8 132 36 32 4 30 8 36 8 57 7 53 13 35 13 25 3 34 6 54 14
40 0 162 2 36 0 38 0 41 1 66 0 64 0 46 0 27 1 35 1 60 2
32 0 151 5 31 3 34 2 41 1 58 2 66 0 45 1 33 1 40 4 66 6