1

I have a program that executes some commands in a terminal (for example apt-get update) and stores the output.

How can I reinterpret the output, so I can remove all information that is not needed ?

For example, the raw output is (this is just a fraction of the output):

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

and it should be interpreted like when you execute the normal command in bash, being the final output:

Reading package lists... Done
  • 1
    looks like you want apt-get -q – glenn jackman Apr 26 '15 at 11:36
  • @glennjackman please note that apt-get is an example, i am looking for a generic solution for any script or command – Tk421 Apr 26 '15 at 21:53
1

Use col -b (on Linux it's part of the util-linux package; in base system elsewhere).

col filters out reverse (and half-reverse) line feeds so the output is in the correct order, with only forward and half-forward line feeds.

| improve this answer | |
1

You recorded the output of the command to a terminal, so it decided to use terminal features. Specifically, apt-get uses carriage return characters to move the cursor back to the beginning of the current line and overwrite the text that was on the line.

To emulate the effect of carriage returns, remove all text before one:

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

If you only want to record the output and not display it to a user, use redirection instead of script or the like. Then apt-get will display plain text.

| improve this answer | |
  • You inspired me to find a easier solution using redirection. If you run from the application whatever-command | tee /dev/null will give you the output formatted like in the terminal. – Tk421 Apr 26 '15 at 22:21
  • 2
    @Tk421 or just whatever-command | cat – Gilles 'SO- stop being evil' Apr 26 '15 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.