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 opene开发者_Python百科d. 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.
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 install vim
; 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:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buffer[1024];
while (fgets(buffer, sizeof(buffer), stdin) != 0)
{
const char *endl = strchr(buffer, '\n') ? "" : "\n";
printf("%s%s", buffer, endl);
}
return 0;
}
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:
linesplitter <build.log > wrapped.build.log
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:
perl -p -e 'while (length($_) > 1024)
{ printf "%.1024s\n", $_; $_ = substr($_, 1024); }'
There might be a more compact way of writing that, but it works which is 90% of the battle. You can test it with:
perl -e 'print "abcdefgh" x 300, "\n"' | ...
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 least nawk
). You might be able to use sed
. You might be able to use pr
. There might be a program fmt
.
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 by vi
. 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 using vim
, so maybe you should install that?
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?
精彩评论