我目前正在使用此命令./myprogram 2> error.log
将程序错误记录到特定文件中。然而,有些行太长,我必须不断滚动才能阅读它们。我想在错误日志中设置行长度限制(例如每行 80 个字符),其中超出限制的行将在下一行继续,等等。
原始内容:
This line is not over 80 characters.
This line is over 80 characters thus will be cut at the 80th character, and will continue onto the next line.
错误日志(最终结果):
This line is not over 80 characters.
This line is over 80 characters thus will be cut at the 80th character, and will
continue onto the next line.
答案1
您可以通过管道将程序的输出以fold
80 个字符的宽度包装输出,然后将其重定向到文件:
./myprogram 2>&1 >/dev/null | fold -w 80 > error.log
上面假设您只关心保留 stderr。