Ubuntu – How to convert script command output into plain text

bashcommand lineconsolegnome-terminalscripts

I am using script-command to save the output of terminal to file.

but, it gives some extra characters like square-like characters and other text encrypted characters.

man script command tells, by using -q flag/option, it gives quite an output, without writing start and done messages to standard output, but it won't remove extra characters.

here

how can I get following:

1) make output file exactly same as of terminal output, removing extra characters.

2) In generated file, due to plain text-without color(unlike terminal), I can't figure-out directly input commands, because output and command I entered appears of same font,

can I make command as bold or easily recognizable than rest output?

Best Answer

  • 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.