4

我有一个如下的数据文件:

----------------------------
a b c d e .............
A B C D E .............
----------------------------

但我希望它采用以下格式:

----------------------------
a A
b B
c C
d D
e E
...
...
----------------------------

在 Vim 或 Perl 中进行转换的最快方法是什么?

4

3 回答 3

6

Basically :.s/ /SpaceCtrl+vEnter/gEnterjma:.s/ /Ctrl+vEnter/gEnterCtrl+v'axgg$p'adG will do the trick. :)

OK, let's break that down:

  1. :.s/ /Ctrl+vEnter/gEnter: 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).
  2. j: Go one line down (to A B C D E).
  3. ma: Set mark a to the current position... because we want to refer to this position later.
  4. :.s/ /Ctrl+vEnter/gEnter: 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).
  5. Ctrl+v'a: Select from the current cursor position (E) to mark a (that we set in step 3 above), using the block select.
  6. x: Cut the selection (into the " register).
  7. gg: Move the cursor to the first line.
  8. $: Move the cursor to the end of the line.
  9. p: Paste the previously cut text after the cursor position.
  10. 'a: Move the cursor to the a mark (set in step 3).
  11. 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...

于 2012-07-18T20:00:20.453 回答
4

假设光标位于两行的第一行,我会使用命令

:s/ /\r/g|+&&|'[-;1,g/^/''+m.|-j
于 2012-07-20T13:41:54.907 回答
4

列的简单重新映射:

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
}

用法:

perl script.pl input.txt
于 2012-07-18T19:12:23.393 回答