How to convert script command output into plain text

If you checkout the:

man script | grep -i bugs -A 2

You can see that:

script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.


What I came up with is a combination of tr and sed to get what we want.

Most of those characters are indicating colors, to remove most of them we can use this sed command from here:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"

However it's still not enough... because as we saw in script's man there are other not printable characters we should get rid of. To remove those we can simply use:

tr -dc '[[:print:]]\n'

Now we are almost there, the other problem in my test was that prompt being repeated twice, so to remove that start script with dump and TERM's value TERM=dump.


Final result:

  1. To capture commands:

    TERM=dump script my_output
    
  2. To remove unnecessary characters:

    sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" my_output | tr -dc '[[:print:]]\n' > new_output
    

For second part of your question, the output file is just a text file so we can't change the font or size of specific lines of it, however your output contains your prompt so you'll know which line is the command and which one is the output.