如何从grep中包装长结果

时间:2013-05-11 14:01:56

标签: bash textwrapping uniq

请原谅我的无知。我是Unix的新手。这可能听起来很傻。 我在包含一些日志详细信息的文件上运行以下命令。

  

cat ./Logfile | uniq -c

现在,当文件包含超出屏幕宽度的超长行时,它会在文本查看器上为用户创建一个水平滚动条。

有没有我可以将文本包装到下一行,以便结果看起来清晰易读并且对用户滚动友好?看起来像下面的东西。

23  This is just a sample result text that has come up to be very long but was 
    WRAPPED to the next line so that the results are displayed in a scroll fri
    endly format. Is that really possible?

2 个答案:

答案 0 :(得分:2)

您可以使用fold实用程序,例如:

$ cat text
This is just a sample result text that has come up to be very long but was WRAPPED to the next line so that the results are displayed in a scroll friendly format. Is that really possible?

$ fold -s -w 80 text
This is just a sample result text that has come up to be very long but was
WRAPPED to the next line so that the results are displayed in a scroll friendly
format. Is that really possible?

答案 1 :(得分:1)

您使用的终端是什么?你在哪里看到这些滚动条?

通常输出应该按预期包装,除了换行从行的开头开始而不是对齐,所以我希望你从命令中得到以下输出:

 23  This is just a sample result text that has come up to be very long but was 
WRAPPED to the next line so that the results are displayed in a scroll fri
endly format. Is that really possible?

编辑: 谢谢你的澄清,现在我了解你的情况。 我认为你可以像其他人在这里建议的那样使用这个折叠,但是在uniq之后管道它:

cat ./Logfile | uniq -c | fold -s -w 80 > result.txt