如何在 Linux 中查看巨大的 txt 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21246752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:53:08  来源:igfitidea点击:

How to view huge txt files in Linux?

linuxeditor

提问by user3213163

I have got a 4 GiBtxt file that I need to view.

我有一个需要查看的4 GiBtxt 文件。

When I try open it in Geditit loads for a while then crashes.

当我尝试在Gedit 中打开它时,它会加载一段时间然后崩溃。

Have you any ideas of a text editor that I can use to view this file. My OS is Fedora 20.

你对我可以用来查看这个文件的文本编辑器有什么想法吗?我的操作系统是Fedora 20

回答by thelastshadow

Not text editors, but in the command line tail -n 100 ./file.txtwill give you the last 100 lines of a file, head -n 100 ./file.txtwill give you the first 100 lines.

不是文本编辑器,而是在命令行中tail -n 100 ./file.txt会给你一个文件的最后 100 行,head -n 100 ./file.txt会给你前 100 行。

vim in the command line buffers as you read through a file (it doesn't open it in one go) so it's quite effective too.

当您阅读文件时,命令行缓冲区中的 vim(它不会一次性打开它),因此它也非常有效。

回答by Uli K?hler

If viewing is anything you need, you might want to use lesswhich should be included with your distribution. I'm frequently viewing terabyte-sized files with it. There are also the variants of zlessand bzlessfor gzipped/bz2'ed files.

如果查看是您需要的任何内容,您可能想要使用less应该包含在您的发行版中的内容。我经常用它查看 TB 大小的文件。还有gzip 压缩/bz2 文件的zless和变体bzless

emacsshould also work with large files (as an alternative to vim).

emacs也应该处理大文件(作为 vim 的替代品)。

nanois another vimalternative I find easier to use for basic tasks in the default configuration. In a short test run, I could successfully view a 1GB file, however opening it took more than 30 seconds (however after that, it seems to run quite smoothly).

nanovim我发现在默认配置中更容易用于基本任务的另一种选择。在一个简短的测试运行中,我可以成功查看一个 1GB 的文件,但是打开它花了 30 多秒(但之后,它似乎运行得很顺利)。

回答by voy

Try glogg. It worked great for me.

尝试glog。它对我很有用。

Debian:

Debian:

apt-get install glogg

Fedora:

软呢帽:

yum install glogg

回答by simhumileco

You can install Midnight Commander.

您可以安装午夜指挥官

Debian, Ubuntu, etc.:

Debian、Ubuntu 等:

apt-get install mc

Red Hat, CentOS, Fedora, etc.:

红帽、CentOS、Fedora 等:

yum install mc

You can start Midnight Commanderfrom the CLIwith the mccommand. After that you may select and open any file in "view mode" (F3) or in "edit mode" (F4).

您可以使用以下命令从CLI启动Midnight Commander。之后,您可以在“查看模式”()或“编辑模式”()中选择并打开任何文件。mcF3F4

mcis much more efficient when opening and browsing large files than vim. I checked it myself.

mc在打开和浏览大文件时比vim. 我自己查过。

If you want to watch a very large file in your favorite editor, without worrying about the limitations associated with RAM, I suggest you split the file using the splitcommand:

如果你想在你最喜欢的编辑器中观看一个非常大的文件,而不用担心与 RAM 相关的限制,我建议你使用以下split命令拆分文件:

split -n 8 [FILE_NAME]

The above cammand splits the file into 8 parts.

上面的命令将文件分成 8 个部分。

Of course, this solution will only check for some applications. Note that not every file retains its consistency after the division (e.g. XMLdoes not). It all depends on what you want to do with the given file.

当然,此解决方案只会检查某些应用程序。请注意,并非每个文件在分割后都保持其一致性(例如XML不会)。这完全取决于您想对给定文件做什么。

回答by JenyaKh

I have just encoutered this very problem -- I need to view big log files. I tried gedit but it failed to open. Vim worked but I want to scroll. I also tried to install gglog but it does not provide the function of line wrapping and my logs are jsonlines so the lines are long and the wrapping is imporatant.

我刚刚遇到了这个问题——我需要查看大日志文件。我试过 gedit 但它无法打开。Vim 工作,但我想滚动。我也尝试安装gglog,但它不提供换行功能,我的日志是jsonlines,所以行很长,换行很重要。

So finally, I unexpectedly discovered that it is the easiest to use a web browser. I checked both Firefox and Chromium. They work perfectly. I can scroll quite comfotably through my big logs and even search for words. So my advice is to use any decent web browser to view such files.

所以最后,我意外地发现它是最容易使用的网络浏览器。我检查了 Firefox 和 Chromium。他们工作得很好。我可以非常舒适地滚动浏览我的大日志,甚至可以搜索单词。所以我的建议是使用任何像样的网络浏览器来查看这些文件。

回答by Douwe Vos

Natpad handles opening big files pretty well. Natpad 2.5

Natpad 可以很好地处理打开大文件。Natpad 2.5