24

Is there any easy way to convert bash output to HTML? For example, if I have some colorized output in bash (something like htop), how can I convert it to HTML, with corresponding stylings/tags/css/etc.

4
  • 1
    What would the conversion results look like? There is no recognizable structure here that could be converted into HTML tags. What exactly are you trying to achieve? Commented Jan 9, 2010 at 12:08
  • Your data seems to be full of ANSI control sequences. Is that intentional? Commented Jan 9, 2010 at 12:09
  • 2
    Related: stackoverflow.com/questions/245121/… Commented Jan 9, 2010 at 12:20
  • If you are using Konsole terminal emulator you can File -> Save Output As... then choose File type: HTML document, to save everything to HTML Commented Jan 24, 2023 at 13:53

5 Answers 5

28

There's ansifilter plus some tools like highlight will produce colorized html from plain text such as source files.

Both available here.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I want ... ansifilter -i file -H -o file.html
10

Yes, you need to pipe the result through a tool like ansi2html.

Comments

6

As suggested in @eli's answer, you can use script to capture the colored output to a file.

You can then convert the output to HTML with aha, the "Ansi HTML Adapter", which should be available in your distribution's repositories: apt install aha or yum install aha for Debian-based or Redhat-based distributions.

If you want to capture only a single command, use the -c option:

script -c "grep --color ..."

This will save the output to a file named typescript in your current directory.

To save to a different file, add the file name at the end:

script -c "grep --color ..." my_grep_colored_output

See man script for other options.

To capture several commands from an interactive session, just start script without a command, and enter Ctrl-d to exit script when done.

To just view the file in color, use less -R.

To convert it to HTML with aha :

aha -s -f typescript > output.html

or

aha -s < typescript > output.html

The -s option makes it write a style sheet in the html header instead of using inline styles through the file. That makes it easier to change colors if some background/foreground combinations turn out hard to read in a browser.

1 Comment

aha did the trick for me, and was available in the debian repositories, thanks!
3

Without any pretty-printing, the simplest thing you can always do is to escape everything that needs escaping, and wrap a basic HTML shell around (the following should be valid minimal HTML5). For example, get a hold of fastesc: http://raa.ruby-lang.org/project/fastesc/, and that wrap it into an HTML shell.

If you want to preserve the ANSI magic, then you need to convert that to HTML, perhaps with http://ansi-sys.rubyforge.org/

And then do something like this, depending on your needs:

require 'ansisys'


def ansi_escape(string)
    terminal = AnsiSys::Terminal.new
    terminal.echo(string)
    terminal.render 
end

def to_html(string)
    %Q{ <!DOCTYPE html>
        <title>Converted to html</title>
        <pre>
        #{ansi_escape(string)}
        </pre>
    } 
end

1 Comment

This probably still needs HTML escaping.
2

1.) check that ansifilter and script programs are installed

2.) script -c 'ansible-playbook -i /tmp/or/some /tmp/other/command.yml' -O /tmp/myTypescriptOutput.file

(script writes the output of a command to a file in typescript notation)

3.) convert to html, e.g. to view with your browser:

ansifilter -i /tmp/myTypescriptOutput.file -H -o output.html

convert to rtf, e.g. to view with libreoffice or word

ansifilter -i /tmp/myTypescriptOutput.file -R -o output.rtf

see man ansifilter for other options (latex, tex, svg, pangot, txt, ...)


(additional, if highlight is installed:

highlight /tmp/myTypescriptOutput.file --force

... this will print the saved-shell output in color, like if it was executed at first time)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.