【问题标题】:How do I add indentation to bash tail?如何在 bash tail 中添加缩进?
【发布时间】:2014-06-15 04:22:48
【问题描述】:

当你在长线上做一个尾巴时,会发生换行。但是,当您跟踪日志时,重要的是要查看一行的结束位置和另一行的开始位置。那么,是否有可能在尾部创建一个标识?换行不是从第 0 列开始,而是从第 10 列开始。例如:

this is a very long line to simulate how a line would wrap in a terminal window
suppose this is the wrapping and it is just the same line the continues here.
this is another very long line to simulate how a line would wrap in a terminal window
suppose this is the wrapping and it is just the same line the continues here.

this is a very long line to simulate how a line would wrap in a terminal window
    suppose this is the wrapping and it is just the same line the continues here.
this is another very long line to simulate how a line would wrap in a terminal window
    suppose this is the wrapping and it is just the same line the continues here.

请注意,我并没有尝试修改该行保存到日志文件的方式,而是仅使用不同的格式显示该行。

【问题讨论】:

  • cat 具有 -n-E 选项以提高这种情况下的可读性。不完全是问什么,但仍然有用。

标签: bash tail


【解决方案1】:

只需将尾部连接到您选择的格式化程序即可。一个简单的 perl 脚本应该可以工作:

tail log-file | perl -pe 's/(.{80})/$1\n\t/g'

将缩进带有制表符的行。如果您正在使用tail -f,您可能希望通过以下方式最小化缓冲:

tail -f log-file | perl -pe '$|=1; s/(.{80})/$1\n\t/g'

【讨论】:

    【解决方案2】:

    如果:

    • 您使用的是 GNU fmt (Linux)
    • 3 个空格的固定缩进也是可以接受的
    • 您的日志行每 N 个字符至少有 1 个制表符或空格。其中 N 是选择的最大值。线宽

    试试,例如:

    tail -f log | fmt -t
    
    • 在达到最大值后换行。默认为 75 个字符。要指定自定义宽度,请使用-w;例如:-w 80
    • fmt 不会在单词中间分裂,这有助于提高可读性。但不幸的是,如果没有空格或制表符,它根本不会拆分。

    【讨论】:

      猜你喜欢
      • 2022-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2021-08-09
      • 2013-06-26
      • 2015-05-11
      • 1970-01-01
      相关资源
      最近更新 更多