2

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.

2

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

  • 1
    Hi, @Thufir. The above commands doesn't work satisfactorily, at the moment. Nevertheless, there is a difference between the output posted in paste.ubuntu.com/25830319 and this one from the question. According to that, please provide also a screenshot and the output of the command locale. – pa4080 Oct 31 '17 at 18:29
  • @Thufir, I've updated the answer with a complete solution. – pa4080 Oct 31 '17 at 22:53
  • Huge effort on your part, thank you. When I can I'll go through it. I don't doubt its correctness. – Thufir Nov 1 '17 at 0:57
1

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.

  • doesn't work: paste.ubuntu.com/25859482 possibly there's a way to tell cat to take out the control characters.. – Thufir Oct 31 '17 at 15:43
  • @Thufir Firstly, if you're running gradle in a script session, you should disable all the fancy stuff it does with its output. It looks like you can use gradle --console=plain to do that. – wjandrea Oct 31 '17 at 17:10
  • @Thufir I guess I didn't explain very well, so I've edited my answer to clarify. – wjandrea Oct 31 '17 at 17:27
  • 1
    Wjandrea, @Thufir wants to use the output file, generated by the command script, with some upload client program as pastebinit. It took me a while to understand that. – pa4080 Nov 1 '17 at 6:41
  • @pa4080 OK, then my answer is not ideal, but it works. Once you have the text in the clipboard, you could paste into a new file, then upload the file to pastebin, or you could use something like xclip to pipe the clipboard out. In any case, the main idea of my answer is to have the terminal do the heavy lifting. – wjandrea Nov 1 '17 at 6:54

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.