首页 文章

如何以行号分割[关闭]

提问于
浏览
91

我想从特定的行号中拆分一个400k行的长日志文件 .

对于这个问题,让我们这个任意数字300k .

是否有一个允许我这样做的linux命令( within the script )?

我知道 split 允许我按大小或行号分割文件,但这不是我想要的 . 我想要一个文件中的第一个300k和第二个文件中的最后一个100k .

任何帮助,将不胜感激 . 谢谢!

On second thoughts this would be more suited to the superuser or serverfault site.

1 回答

  • 175
    file_name=test.log
    
    # set first K lines:
    K=1000
    
    # line count (N): 
    N=$(wc -l < $file_name)
    
    # length of the bottom file:
    L=$(( $N - $K ))
    
    # create the top of file: 
    head -n $K $file_name > top_$file_name
    
    # create bottom of file: 
    tail -n $L $file_name > bottom_$file_name
    

    此外,在第二个想法,拆分将适用于您的情况,因为第一次拆分大于第二次拆分 . 拆分将输入的余额放入最后一个拆分中,所以

    split -l 300000 file_name

    输出带有300k线的 xaa 和带有100k线的 xab ,用于400k线的输入 .

相关问题