【问题标题】:Will the tail '-c' parameter only return the specified number of bytes of the line? Or will it return the entire line in chunks of -c bytes?tail '-c' 参数是否只返回行的指定字节数?或者它会以 -c 字节块的形式返回整行?
【发布时间】:2014-10-03 19:04:15
【问题描述】:

我有一个接收最多 8k 字节的消息的日志文件。我希望这些消息更小。我目前正在使用tail -c +1 -F 跟踪日志文件,但是这会抓取整个 8k 消息,这非常大。我想做类似tail -c 2048 -F 之类的事情,但是我不想丢失消息中的文本。如果味精超过 1024 字节,tail -c 1024 -F 会导致我丢失一些味精吗?

附加信息: 我正在从 python 调用 tail 命令,如果消息太大,我的正则表达式过滤会滞后。

for line in sh.tail("-c", "4096", "-F", path, _iter=True):
    # Doing some regex filtering here

【问题讨论】:

  • 什么对象有tail方法?
  • 我们可以在你的代码中看到的,你不是“从 python 调用 tail 命令”,你将一些参数传递给一些 tail 方法sh 对象。
  • sh 是一个允许调用某些系统程序的包。以下是文档:amoffat.github.io/sh/#

标签: python unix logging tail


【解决方案1】:

tail -n +2 打印从第二行到最后的所有内容。 您正在寻找的是 tail -n 2 或更短:tail -2,它将占据最后两行。

同样的架构适用于字节:

正如man tail 所说:

-c, --bytes=K output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file

RTFM?

【讨论】:

猜你喜欢
  • 2012-08-05
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
相关资源
最近更新 更多