Vim:根据行数

时间:2016-11-09 14:16:09

标签: windows vim

我有一个包含5,000行的文本文件。我需要将其拆分为每个不超过99行的文件。我可以用vim做这个吗?如果没有,我的其他选择是什么?

3 个答案:

答案 0 :(得分:11)

首先,定义一个控制变量:

:let i = 1

然后,将第1行到第99行(包括)写入以控制变量的当前值命名的文件,剪切这些行,并递增控制变量;

:exec "1,99w! chunk-" . i|1,99d|let i = i + 1

根据需要重复多次:

49@:

这应该会为您提供50个名为chunk-1chunk-50的文件。

由于5000不能被99整除,你将留下50行。将它们写入chunk-51

:w chunk-51

答案 1 :(得分:5)

有一个名为split :)的工具会为你做这个

示例:

  split -a 3 -d -l 99 my_big_file.txt big_file_chunk_ 

  -a 3 : says to use a unique 3 character suffix for each chuck file
  -d   : says make that suffix a number so 001 002 all the way to 999
  -l 99: split file by line and have 99 lines or less in each chuck.

  next are the source file name and if you want the prefix to use for each produced file.

这将创建多个文件,其中最多99行来自原始名称

   big_file_chunk_001
   big_file_chunk_002
   ....

答案 2 :(得分:0)

像(未经测试的)。

let lines = getline(1,'$')
let nb_files = len(lines) / nb_rows
for i in range(0, nb_files)
    call writefile(lines[(i*nb_rows) : min([(i+1)*nb_rows-1, len(lines)-1])], 'chunk_'.i
endfor

let lines = getline(1,'$')
while len(lines)
   call writefile(remove(lines,min([99,len(lines)-1])), 'chunk-'.i)
endw
相关问题