Text-Processing

重新解釋終端輸出

  • July 20, 2015

我有一個程序在終端中執行一些命令(例如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

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

col 過濾掉反向(和半反向)換行符,因此輸出順序正確,只有正向和半正向換行符。

您將命令的輸出記錄到終端,因此它決定使用終端功能。具體來說,apt-get使用輸入符將游標移回目前行的開頭並覆蓋該行上的文本。

要模擬輸入的效果,請刪除前面的所有文本:

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

如果您只想記錄輸出而不將其顯示給使用者,請使用重定向而不是script等。然後apt-get將顯示純文字。

引用自:https://unix.stackexchange.com/questions/198696