在VIM中处理大型文件 [英] Working with huge files in VIM

查看:104
本文介绍了在VIM中处理大型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在VIM中打开一个巨大的文件(〜2GB),但它被阻塞了.我实际上不需要编辑文件,只需高效地跳转即可.

I tried opening a huge (~2GB) file in VIM but it choked. I don't actually need to edit the file, just jump around efficiently.

如何在VIM中处理非常大的文件?

How can I go about working with very large files in VIM?

推荐答案

我今天有12GB的文件要编辑. vim LargeFile插件对我不起作用.它仍然用完了我所有的内存,然后显示一条错误消息:-(.我也不能使用hexedit,因为它不能插入任何东西,只能覆盖.这是另一种方法:

I had a 12GB file to edit today. The vim LargeFile plugin did not work for me. It still used up all my memory and then printed an error message :-(. I could not use hexedit for either, as it cannot insert anything, just overwrite. Here is an alternative approach:

您分割了文件,编辑了零件,然后重新组合.但是,您仍然需要两倍的磁盘空间.

You split the file, edit the parts and then recombine it. You still need twice the disk space though.

  • Grep查找您要编辑的行周围的内容:

  • Grep for something surrounding the line you would like to edit:

grep -n 'something' HUGEFILE | head -n 1

  • 提取该文件的范围.说您要编辑的行在第4行和第5行.然后执行以下操作:

  • Extract that range of the file. Say the lines you want to edit are at line 4 and 5. Then do:

    sed -n -e '4,5p' -e '5q' HUGEFILE > SMALLPART
    

    • 需要-n选项以抑制sed的默认行为以打印所有内容
    • 4,5p打印第4行和第5行
    • 5q在处理第5行后中止sed
      • The -n option is required to suppress the default behaviour of sed to print everything
      • 4,5p prints lines 4 and 5
      • 5q aborts sed after processing line 5
      • 使用您喜欢的编辑器编辑SMALLPART.

        Edit SMALLPART using your favourite editor.

        合并文件:

        (head -n 3 HUGEFILE; cat SMALLPART; sed -e '1,5d' HUGEFILE) > HUGEFILE.new 
        

        • ie:从HUGEFILE(在本例中为前3行)中选择所有已编辑行之前的所有行,将其与已编辑行(在本例中为第4和5行)合并,并使用这组组合的行替换HUGEFILE中的等效项(在本例中为前5行),并将其全部写入新文件中.
        • HUGEFILE.new现在将成为您的编辑文件,您可以删除原始的HUGEFILE.

          HUGEFILE.new will now be your edited file, you can delete the original HUGEFILE.

          这篇关于在VIM中处理大型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆