重新解释终端输出

重新解释终端输出

我有一个程序,可以在终端(例如apt-get update)中执行一些命令并存储输出。

如何重新解释输出,以便删除所有不需要的信息?

例如,原始输出是(这只是输出的一小部分):

Reading package lists... 0%

Reading package lists... 0%

Reading package lists... 1%

Reading package lists... 6%

Reading package lists... 6%

Reading package lists... 26%

Reading package lists... 32%

Reading package lists... 32%

Reading package lists... 39%

Reading package lists... 39%

Reading package lists... 53%

Reading package lists... 65%

Reading package lists... 65%

Reading package lists... 68%

Reading package lists... 68%

Reading package lists... 83%

Reading package lists... 84%

Reading package lists... 84%

Reading package lists... 86%

Reading package lists... 86%

Reading package lists... 88%

Reading package lists... 88%

Reading package lists... 90%

Reading package lists... 90%

Reading package lists... 92%

Reading package lists... 92%

Reading package lists... 93%

Reading package lists... 93%

Reading package lists... 94%

Reading package lists... 94%

Reading package lists... 95%

Reading package lists... 95%

Reading package lists... 96%

Reading package lists... 96%

Reading package lists... 97%

Reading package lists... 97%

Reading package lists... 98%

Reading package lists... 98%

Reading package lists... 99%

Reading package lists... 99%

Reading package lists... 99%

Reading package lists... 99%

Reading package lists... Done

它应该像在 bash 中执行普通命令一样解释,即最终输出:

Reading package lists... Done

答案1

使用col -b(在 Linux 上它是 util-linux 包的一部分;在其他地方的基本系统中)。

col 过滤掉反向(和半反向)换行,因此输出的顺序正确,只有正向和半正向换行。

答案2

您将命令的输出记录到终端,因此它决定使用终端功能。具体来说,apt-get使用回车符将光标移回当前行的开头并覆盖该行上的文本。

要模拟回车的效果,请删除一个之前的所有文本:

<transcript sed 's/.*\r//'

如果您只想记录输出而不将其显示给用户,请使用重定向等script。然后apt-get将显示纯文本。

相关内容