如何在vi编辑器中打开一个50k行的文件?
我正在尝试在 Solaris vi 编辑器中打开日志文件,但只打开了 4k 行。如果我使用 less 打开同一个文件,所有行都会正确打开。如果我对该文件执行 wc 命令,
# wc -l build_log
52879 build_log
请给我一个在 vi 编辑器中完全打开该文件的解决方案。
I am trying to open a log file in solaris vi editor, only 4k lines getting opened. If i open that same file using less, all lines got properly opened. If I execute the wc command on that file,
# wc -l build_log
52879 build_log
Please give me a solution to open that file completely in vi editor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于(根据注释)
vi
告诉您“行太长”,因此您必须找到一种方法将超长行包装在文件中,以便“vi”可以处理他们。或者安装vim
;我没有注意到它对行长度有限制。怎么包?
自己编写包装器可能是最简单的 - 可能有内置的工具可以完成这项工作,但您必须找到合适的工具。这是一个用 C 语言编写的粗略但有效的纯过滤器 - 我编写它的时间与键入它的时间一样长:
它读取长达 1023 字节的行并将其原封不动地吐出。较长的线会被分开;当它读取该行的初始段时,缓冲区末尾没有换行符,因此我们人为地添加一个。这会包裹超长线。将其编译为“linesplitter”; use as:
显然,您可以增强它来处理文件列表或接受参数来控制行分割的长度。如果你想变得更奇特,你可以在空白处分割。有无数的工具也可以完成这项工作;我知道我自己有几个可以完成此任务的不同变体的代码,这些代码是在 80 年代中期以来的不同时间编写的。
珀尔?
如果您愿意(您可能应该这样做),您可以使用 Perl 来代替:
可能有一种更紧凑的编写方式,但它可以解决 90% 的问题。您可以使用以下命令进行测试:
生成一行 2400 字节; linesplitter 程序和 Perl 脚本都输出 3 行数据,但输出有所不同,因为一个将行限制为 1023 个字节,另一个将行限制为 1024 个字节(加上换行符)。
其他的?
Perl 能做的事,Python 也能做。您也许可以使用
awk
(至少nawk
)。您也许可以使用sed
。您或许可以使用pr
。可能有一个程序fmt
。用什么?
根据您使用不同工具的技能水平,我建议 Perl 足以胜任这项工作。如果您熟悉编译 C 程序,那么 C 语言也几乎是微不足道的。我整天都这样做,所以这就是我的思维方式。
请注意,该解决方案明确地解决了由
vi
诊断的vi
中的限制。我 99.9% 确信它可以轻松处理 1 KiB 行;它可以很好地处理更长的线路。您选择要分割的号码。而且我不必使用 vim 重新格式化甚至相当可怕的构建日志,所以也许你应该安装它?Since (according to the comments)
vi
tells you 'line too long', you will have to find a way to wrap the ultra-long line(s) in the file so that 'vi' can handle them. Or installvim
; I've not noticed it have limits on line length.How to wrap?
It might be simplest to write the wrapper yourself - there probably are tools built in to do the job, but you have to find the right one. This is a crude but effective pure filter written in C - it took me as long to write it as it took to type it:
It reads lines up to 1023 bytes long and spits those back out unchanged. Longer lines get split; as it reads the initial segments of the line, buffer has no newline at the end, so we artificially add one. This wraps ultra-long lines. Compile it as 'linesplitter'; use as:
Clearly, you can enhance it to process lists of files or accept arguments to control the length at which lines split. If you want to get fancy, you can split at white space. There are a myriad tools that will probably also do the job; I know I have several of my own that do different variants of this task, written at various times since the mid-80s.
Perl?
If you prefer (you probably should), you can use Perl instead:
There might be a more compact way of writing that, but it works which is 90% of the battle. You can test it with:
That generates a line with 2400 bytes; the
linesplitter
program and the Perl script both spit out 3 lines of data, though there's a difference in the outputs because one limits the lines to 1023 and the other to 1024 bytes (plus newline).Others?
What can be done in Perl can be done in Python. You might be able to use
awk
(at leastnawk
). You might be able to usesed
. You might be able to usepr
. There might be a programfmt
.What to use?
Depending on your skill level with the different tools, I suggest that the Perl is quite adequate for the job. The C is close to trivial too if you're familiar with compiling C programs; I do that all day so it is the way my mind tends to work.
Note that the solution is unequivocally working around a limitation in
vi
diagnosed byvi
. I'm 99.9% sure it will handle 1 KiB lines with ease; it might well handle quite a bit longer lines. You choose the number to split on. And I've not had to reformat even fairly ghastly build logs usingvim
, so maybe you should install that?wc 和 vi 处理不同长度的线。虽然 vi 可以打开很长的文件,但在这种情况下,您更有可能有很长的行(未在 vi 中显示)。我们无法确定,因为您只向我们提供了有限的数据。但请查看以下SO线程:
vim 中最长的行?
wc and vi handle lines of different length. While vi can open very long files, in this case it's more likely that you have long lines (not being displayed in vi). We can't be sure because you've only provided us limited data. But check out the following SO thread:
longest line in vim?