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.


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?


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.

  • thanks, but only small doubt is left, still ]0; is appearing before terminals username. screenshot here imgur.com/a/MWPpBLb – GD- Ganesh Deshmukh Aug 11 '18 at 8:25
  • 1
    If you use TERM=dump script to run script it shouldn't appears anymore... yo remove ]0; you can use sed 's/^]0;//' output it should do the job. – Ravexina Aug 11 '18 at 8:37
  • thanks, but still sed 's/^]0;//' filename didn't work. I also tried sed 's/^]0;//g' . I didn't used TERM=dump script as I am appending my output to previously created file, and I am using script command. – GD- Ganesh Deshmukh Aug 11 '18 at 8:53
  • 2
    use TERM=dump script -a to append... – Ravexina Aug 11 '18 at 9:02

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.