2016-11-09 53 views
2

我有一個包含5000行的文本文件。我需要將它分成不超過99行的文件。我可以用vim做這個嗎?如果不是,我的其他選擇是什麼?Vim:根據行數將一個文件拆分爲多個文件

+0

'男人split'是你的朋友 –

+0

我本來應該更清晰,我一直在尋找一種方式用vim做到這一點在Windows中。 – divided

+0

你有Cygwin嗎?它將許多Linux工具帶入Windows,它已經分裂。 – Rob

回答

8

首先,定義一個控制變量:

:let i = 1 

然後,將第1到99行(包含)寫入以控制變量的當前值命名的文件,剪切這些行並遞增控制變量;根據需要

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

重複多次:

[email protected]: 

這應該給你一個名爲chunk-1chunk-50 50個文件。

由於5000不能被99整除,所以你將剩下50行。寫他們到chunk-51

:w chunk-51 
4

有一個叫分裂:)工具,將做到這一點的,你

例如:

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. 

這將從原來的命名

big_file_chunk_001 
    big_file_chunk_002 
    .... 
創建最多99行多個文件
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 
相關問題