试图调试服务器的一个问题,我唯一的日志文件是一个20GB的日志文件(甚至没有时间戳!)为什么人们使用System.out.println()作为日志?在生产吗? !)

使用grep,我找到了我想要查看的文件区域,第347340107行。

除了做一些

head -<$LINENUM + 10> filename | tail -20 

... 这将需要头读取日志文件的前3.47亿行,是否有一个快速而简单的命令将行347340100 - 347340200(例如)转储到控制台?

我完全忘记了grep可以打印匹配前后的上下文…这很有效。谢谢!


当前回答

如果你的行号是100来读取

head -100 filename | tail -1

其他回答

用sed -e '1,N d;M q'你将打印从N+1到M的行,这可能比grep -C更好一点,因为它不会尝试将行与模式匹配。

是什么:

tail -n +347340107 filename | head -n 100

我没有测试,但我认为这是可行的。

# print line number 52
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3,  efficient on large files 

方法3对大文件有效

显示特定线条的最快方式

得到证实

Ubuntu/Debian 安装:

$ sudo apt-get install ack-grep

然后运行:

$ ack --lines=$START-$END filename

例子:

$ ack --lines=10-20 filename

来自$ man ack:

--lines=NUM
    Only print line NUM of each file. Multiple lines can be given with multiple --lines options or as a comma separated list (--lines=3,5,7). --lines=4-7 also works. 
    The lines are always output in ascending order, no matter the order given on the command line.

不,没有,文件不是行寻址的。

在文本文件中,没有固定时间的方法来找到第n行的开始。您必须流遍文件并计算换行符。

使用你拥有的最简单/最快的工具来完成这项工作。对我来说,使用head比使用grep更有意义,因为后者要复杂得多。我并不是说“grep很慢”,它确实不是,但是如果它比head更快,我会感到惊讶。那基本上就是脑袋里的虫子了。