linux - 在 Bash 中从最后到第一个输出文件行

标签 linux bash shell tail

我想显示我的日志文件的最后 10 行,从最后一行开始——就像普通的日志阅读器一样。我认为这将是 tail 命令的变体,但我在任何地方都找不到。

最佳答案

GNU (Linux) uses the following :

tail -n 10 <logfile> | tac

tail -n 10 <logfile>打印出日志文件的最后 10 行和 tac (cat 向后拼写)颠倒顺序。

BSD (OS X)tail使用 -r选项:

tail -r -n 10 <logfile>

对于这两种情况,您可以尝试以下方法:

if hash tac 2>/dev/null; then tail -n 10 <logfile> | tac; else tail -n 10 -r <logfile>; fi

注意: GNU manual states BSD -r选项“只能反转最多与其缓冲区一样大的文件,通常为 32 KiB”和 tac更可靠。如果缓冲区大小有问题并且您不能使用 tac ,您可能需要考虑使用 @ata's answer它在 bash 中编写功能。

关于linux - 在 Bash 中从最后到第一个输出文件行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8017456/

相关文章:

linux - 创建一个 bash 脚本,为文件中的每个名称创建一个文件夹

c - 内置函数 bzero 的不兼容隐式声明

java.lang.UnsatisfiedLinkError : no xuggle-xuggler in java. 库路径

linux - bash linux 中的 Ceil 只有 float

c++ - 在/usr/local/lib 中为 C++ 安装 xmlrpc

linux - grep : print value when grep returns nothing 之后的 awk

reactjs - Yarn 可以列出所有可用的脚本吗?

shell - 无法理解命令 find 的语法

linux - grep字符串中的多个通配符

linux - 如果在 shell 脚本中给出了子 ID,如何找到父 ID