我有一个 23GB 的文件,我想编辑第 23 行,但服务器上只有 200 MB 可用 RAM。我不想完全打开该文件,因为我只剩下 20GB 可用磁盘空间。
我怎样才能做到这一点。我尝试使用 head, tail sed 但它似乎创建了一个临时文件。是否可以在没有临时文件的情况下做到这一点?
解决方案是使用十六进制编辑器编辑该文件。十六进制编辑器旨在处理大文件,甚至整个磁盘和分区。
你可能会发现hexedit http://www.rogoyski.com/adam/programs/hexedit/(基于 ncurses)或ghex https://developer.gnome.org/ghex/(基于 Gnome/Gtk)有用。它们是常见的实用程序,因此您很可能会在发行版的存储库中找到它们。
我使用过的所有十六进制编辑器都使用双面板视图,左侧面板以十六进制显示文件的字节,右侧面板在可能的情况下尝试显示 Ascii 表示形式。
为了找到并编辑第 23 行:
sed -n '23p' my_huge_dump.sql
:将打印这一行的内容
sed -n '23p' my_huge_dump.sql | od -A n -t x1
:将以十六进制格式打印该行的内容。
或使用以下命令打开文件less -N my_huge_dump.sql
并查看第23行的内容。(-N
in less
启用行编号)
现在,了解第23行的内容:
- If the text of this line is somewhat unique and different from surrounding lines, you may find it from the right (ascii) panel and navigate to this line with the arrows. In
hexedit
you use the Tab key to move between the Hex and Ascii panels. In gHex you can use your mouse as well. You may also search for the string you're interested: Move to the Ascii panel and press / in hexedit
or use the menu in gHex.
- 如果你要编辑的行与其他行有相似的内容,而你在ascii面板中找不到它,那么你必须计算“换行符”分隔符才能找到第23行。新行(LF)表示为
0A
以十六进制表示。在 ASCII 面板中,新行表示为点.
然后假设您找到了要编辑的行,您有以下选项:
- Hopefully, the new content of the 23rd line is shorter or equal in length to the existing content (so you won't need to grow and move the whole file). In this case, you have to enter the
Fill-mode
i.e. the mode in which you overwrite existing content typing over the old text. This is the default mode in both gHex
and hexedit
. Move to the location you want to edit and start typing. Pressing Backspace will undo your changes. If the new content is shorter than the existing, you may fill up the line with spaces to avoid truncating the file.
- If the new content is longer than the existing one in this line, then you have to enter the
Insert
mode. You can do that using the Menu in gHex. In hexedit
you have to use the EscI keybinding. Then start typing and the new characters will be appended in the current location.
在第一种情况下,可以保证文件的编辑和保存是即时的,因为将发生就地编辑。在后一种情况下,我不确定如何处理大小的增长和后续字节的移动,但我希望文件系统使用更大的非连续块来移动某些内容而不是移动整个文件。
如果您对更改感到满意,请保存文件:
- 使用 gHex 中的菜单
- Use Ctrlx in
hexedit
and answer (Y)es
when questioned about whether to save the changes.
始终确保您有备份!
编辑:我发现gHex
不适合您的情况,因为它尝试将整个文件加载到内存中。hexedit
将为您服务。但是,如果您想要像这样的图形编辑器gHex
,但具有部分文件加载功能,您可以尝试wxHex编辑器 http://www.wxhexeditor.org/home.php。还检查十六进制编辑器的比较 https://en.wikipedia.org/wiki/Comparison_of_hex_editors维基百科中的页面。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)