I've tested all solution, provided within the references below, to process and clean the output file of the command script from special characters.

On my Ubuntu 16.04, only the following solution provides a satisfactory result:

perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' typescript | col -b > typescript.new

Or you can pipe the output directly to the upload client program:

perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' typescript | col -b | pastebinit

The above command works with pastebinit, so install it:

sudo apt install pastebinit

References:

  • The main source of the answer
  • Remove color codes (special characters) with sed
  • How to strip color codes out of stdout and pipe to file and stdout
  • Removing ANSI color codes from text stream
  • Removing control chars (including console codes / colours) from script output
  • How to set locale in the current terminal's session?

Create a 'script to pastebin' custom command - spaste

I would suggest to create a custom command, based on the above solution. Let's name it spaste.

1. Create executable script file, called spaste, that is located in /usr/local/bin to be accessible as shell command:

sudo touch /usr/local/bin/spaste
sudo chmod +x /usr/local/bin/spaste
sudo nano /usr/local/bin/spaste
  • Copy the script below. And in nano: paste Shift Ins; save CtrlO Enter; exit CtrlX.
#!/bin/bash
# Name: spaste
# Location: /usr/local/bin
#export LC_ALL=C

# If the first input parameter is option - see: script --help; or type `script --help`
[[ "${1}" =~ -.* ]] && TARGET_FILE="$2" || TARGET_FILE="$1"

# If the variable $TARGET_FILE is empty, use the default output file name
[[ -z "${TARGET_FILE}" ]] && TARGET_FILE="typescript"

# The main function - Remove color codes, etc.
script_remove_extras() {
        script "$@"
        perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' "$TARGET_FILE" | col -b > "/tmp/$USER-cpaste-$TARGET_FILE.tmp"
        cp "/tmp/$USER-cpaste-$TARGET_FILE.tmp" "$TARGET_FILE"
}

# Upload to 'pastebinit'
upload_pastebinit() { pastebinit < "$TARGET_FILE"; }

# GUI mode with 'chromium' or 'firefox'; CLI mode with 'lynx'; Just upload with 'pastebinit'; Just clear the outputfile
if   [ "$SPASTE_MODE" == "chromium" ]; then
        script_remove_extras "$@"; nohup chromium-browser "$(upload_pastebinit)" >/dev/null 2>&1 &
elif [ "$SPASTE_MODE" == "firefox" ]; then
        script_remove_extras "$@"; nohup firefox "$(upload_pastebinit)" >/dev/null 2>&1 &
elif [ "$SPASTE_MODE" == "lynx" ]; then
        script_remove_extras "$@"; lynx "$(upload_pastebinit)"
elif [ "$SPASTE_MODE" == "upload" ]; then
        script_remove_extras "$@"; upload_pastebinit
else
        script_remove_extras "$@"
fi

2. Explanation:

  • When you execute the new command spaste it will call the command script and will assign to it the user's input parameters. So the call syntax is the same as the command script - see script --help or type spaste --help for more details:

    Usage:
     spaste (script) [options] [file]
    
    Make a typescript of a terminal session.
    
    Options:
     -a, --append            append the output
     -c, --command <command> run command rather than interactive shell
     -e, --return            return exit code of the child process
     -f, --flush             run flush after each write
         --force             use output file even when it is a link
     -q, --quiet             be quiet
     -t, --timing[=<file>]   output timing data to stderr (or to FILE)
     -V, --version           output version information and exit
     -h, --help              display this help and exit
    
  • When you type exit to exit from the session of the script command, spaste will process the output file of script to the command pastebinit.

  • In result pastebinit will return a link to the uploaded content of the script's output file.

  • The new command spaste has few different modes how to handle the link returned by pastebinit. These modes could be switched by export of the variable $SPASTE_MODE with different values before the execution of the command spaste:

    $ export SPASTE_MODE=
    $ spaste
    
  • The available modes are:

    • SPASTE_MODE=chromium - will open the returned link in Chromium.
    • SPASTE_MODE=firefox - will open the returned link in FireFox.
    • SPASTE_MODE=lynx - will open the returned link Lynx (terminal browser).
    • SPASTE_MODE=upload - just will output the returned link.
    • SPASTE_MODE= - will not return a link; just will process the content of the output file.
  • You could export your favourite mode from the ~/.bashrc file, for example add to its bottom the following line:

    export SPASTE_MODE=firefox
    

3. Demonstration of usage:

enter image description here


I don't think this is the answer you're looking for, but it works:

Once you've run script and it's created a typescript file, run cat typescript. All the escape sequences are consumed by the terminal, so the output is colorized plaintext. Copy it manually (using the mouse), and it will save into the clipboard as plain text. Paste where needed.