\$\begingroup\$
\$\endgroup\$
8
I'm trying to transpose the following data from:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
to:
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
using sed
only please.
I have a working solution but I'm sure it can be improved:
sed -rn 'H;${x;s/\n/ &/g;s/$/@/;:a;s/\n([^ ]+ ?)(.*@.*)/%2円1円/;ta;s/ %+@//p;t;s/ *$/\n/;y/%/\n/;ta}'
It uses %
and @
for newline and end-of-string delimiters which may be problematic.
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 14, 2011 at 1:14
potongpotong
1 Answer 1
\$\begingroup\$
\$\endgroup\$
This yet another way to do it:
sed -r '1{s/$/ /;s/ / \n/g};:a;$!N;s/$/ /;:b;s/\n(.*\n+)(\S+\s)/2円@!@1円/;tb;s/@!@/\n/g;${s/ \n/\n/g;s/\n+$//;q};ba'
This method is somewhat faster and only uses single delimiter which may be crafted to be unique. i.e. @!@
in this example
default
sed
? What aboutawk
? \$\endgroup\$sed
. BTW @Dave this is not homework per se just personal curiosity. \$\endgroup\$