I have a data file like the following:
----------------------------
a b c d e .............
A B C D E .............
----------------------------
But I want it to be in the following format:
----------------------------
a A
b B
c C
d D
e E
...
...
----------------------------
What is the quickest way to do the transformation in Vim or Perl?
Basically :.s/ /
SpaceCtrl+vEnter/g
Enterjma:.s/ /
Ctrl+vEnter/g
EnterCtrl+v'axgg$p'adG
will do the trick. :)
OK, let's break that down:
:.s/ /
Ctrl+vEnter/g
Enter: On the current line (.
), substitute (s
) spaces (/ /
) with a space followed by a carriage return (SpaceCtrl+vEnter/
), in all positions (g
). The cursor should now be on the last letter's line (e
in the example).j
: Go one line down (to A B C D E
).ma
: Set mark a
to the current position... because we want to refer to this position later.:.s/ /
Ctrl+vEnter/g
Enter: Do the same substitution as above, but without the Space. The cursor should now be on the last letter's line (E
in the example).'a
: Select from the current cursor position (E
) to mark a
(that we set in step 3 above), using the block select.x
: Cut the selection (into the "
register).gg
: Move the cursor to the first line.$
: Move the cursor to the end of the line.p
: Paste the previously cut text after the cursor position.'a
: Move the cursor to the a
mark (set in step 3).dG
: Delete everything (the empty lines left at the bottom) from the cursor position to the end of the file.P.S. I was hoping to learn about a "built-in" solution, but until such time...
Simple re-map of the columns:
use strict;
use warnings;
my @a = map [ split ], <>; # split each line on whitespace and store in array
for (0 .. $#{$a[0]}) { # for each such array element
printf "%s %s\n", $a[0]->[$_], $a[1]->[$_]; # print elements in order
}
Usage:
perl script.pl input.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With