如何在 bash 尾部添加缩进?

问题描述 投票:0回答:2

当你在长线上做尾巴时,就会发生包裹。但是,当您跟踪日志时,重要的是要查看一行的结束位置和另一行的开始位置。那么,是否可以在 tail 中创建一个标识?换行不是从第 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.

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

bash tail
2个回答
8
投票

只需通过管道将尾部连接到您选择的格式化程序即可。一个简单的 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'

3
投票

如果

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

尝试,例如:

tail -f log | fmt -t
  • 在最大长度后断行。默认情况下为 75 个字符。要指定自定义宽度,请使用
    -w
    ;例如:
    -w 80
  • fmt
    不会在单词中间分裂,这有助于可读性。但不幸的是,如果没有空格或制表符,它根本不会拆分。
© www.soinside.com 2019 - 2024. All rights reserved.