Ubuntu – Output “script” to pastebin readability

application-developmentcommand linepastebinscripts

How do I paste output from the script command (typescript; "man script") so that it's more readable?

Script started on 2017-10-27 06:20:56-0700
]0;thufir@dur: ~/IdeaProjects[01;32mthufir@dur[00m:[01;34m~/IdeaProjects[00m$ 
]0;thufir@dur: ~/IdeaProjects[01;32mthufir@dur[00m:[01;34m~/IdeaProjects[00m$ tree
[01;34m.[00m
├── kotlin
└── [01;34mkotlinHelloWorld[00m
    ├── kotlinHelloWorld.iml
    ├── [01;34mout[00m
    │   └── [01;34mproduction[00m
    │       └── [01;34mkotlinHelloWorld[00m
    └── [01;34msrc[00m
        └── Main.kt

5 directories, 3 files
]0;thufir@dur: ~/IdeaProjects[01;32mthufir@dur[00m:[01;34m~/IdeaProjects[00m$ 
]0;thufir@dur: ~/IdeaProjects[01;32mthufir@dur[00m:[01;34m~/IdeaProjects[00m$ cat kotlinHelloWorld/src/Main.kt
class Main {



    fun main(args: Array<String>) {
        println("Hello, world!")
    }
}]0;thufir@dur: ~/IdeaProjects[01;32mthufir@dur[00m:[01;34m~/IdeaProjects[00m$ 
]0;thufir@dur: ~/IdeaProjects[01;32mthufir@dur[00m:[01;34m~/IdeaProjects[00m$ exit
exit

Script done on 2017-10-27 06:21:19-0700

The gibberish (?) is reflective of, in this case, gnome-terminal color. Yes, by using a different shell, different console, etc it's possible to not generate that "gibberish". The question is how to nicely either get the script command to not record them, or get the pastebin utility (or similar, such as gist-paste) to handle them "nicely".

firstly, thank you for the responses. Secondly, I hope that this doesn't muddy the waters:

thufir@dur:~$ 
thufir@dur:~$ script trying_to_eliminate_control_chars.txt
Script started, file is trying_to_eliminate_control_chars.txt
thufir@dur:~$ 
thufir@dur:~$ echo "hi"
hi
thufir@dur:~$ 
thufir@dur:~$ echo "hi"
hi
thufir@dur:~$ 
thufir@dur:~$ echo "hmm"
hmm
thufir@dur:~$ 
thufir@dur:~$ exit
exit
Script done, file is trying_to_eliminate_control_chars.txt
thufir@dur:~$ 
thufir@dur:~$ cat trying_to_eliminate_control_chars.txt > foo.txt
thufir@dur:~$ 
thufir@dur:~$ cat foo.txt 
Script started on 2017-10-31 17:51:29-0700
thufir@dur:~$ 
thufir@dur:~$ echo "hi"
hi
thufir@dur:~$ 
thufir@dur:~$ echo "hi"
hi
thufir@dur:~$ 
thufir@dur:~$ echo "hmm"
hmm
thufir@dur:~$ 
thufir@dur:~$ exit
exit

Script done on 2017-10-31 17:51:47-0700
thufir@dur:~$ 
thufir@dur:~$ cat trying_to_eliminate_control_chars.txt 
Script started on 2017-10-31 17:51:29-0700
thufir@dur:~$ 
thufir@dur:~$ echo "hi"
hi
thufir@dur:~$ 
thufir@dur:~$ echo "hi"
hi
thufir@dur:~$ 
thufir@dur:~$ echo "hmm"
hmm
thufir@dur:~$ 
thufir@dur:~$ exit
exit

Script done on 2017-10-31 17:51:47-0700
thufir@dur:~$ 
thufir@dur:~$ pastebin foo.txt 
pastebin: command not found
thufir@dur:~$ 
thufir@dur:~$ pastebinit foo.txt 
http://paste.ubuntu.com/25862228/
thufir@dur:~$ 

While you may naively expect the above paste not to have control characters, it does. This is because the terminal itself (or so I infer) is handling/hiding them.

Very low-level text files, etc, at play. Plus very detailed knowledge of how terminal emulation works. A bit beyond me, frankly.

Best Answer

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:


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

Related Question