如何在bash尾部添加缩进?

时间:2014-06-15 04:22:48

标签: bash 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.

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

2 个答案:

答案 0 :(得分:7)

将管尾拖到您选择的格式化程序中。一个简单的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'

答案 1 :(得分:2)

如果:

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

尝试,例如:

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