The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Turn terminal output into HTML reports

Turning terminal output into HTML reports can help you visualize and share command line results more effectively. Below is a comprehensive guide and example on how to convert typical terminal output into a clean, structured HTML report.


Why Convert Terminal Output to HTML?

  • Improved readability: HTML allows formatting, colors, tables, and charts.

  • Easy sharing: HTML files can be opened in any browser.

  • Customization: Add CSS styles and interactive elements.

  • Automation: Automate report generation from scripts or commands.


General Approach

  1. Capture terminal output
    Run your command and redirect output to a text file or capture it in a script.

  2. Parse the output
    Use a scripting language (Python, Bash, etc.) to read and process the output.

  3. Generate HTML
    Wrap the processed data in HTML tags, add styles, and optionally scripts for interactivity.


Example: Convert ls -l Output to an HTML Report Using Python

Sample terminal output (from ls -l):

sql
-rw-r--r-- 1 user group 2345 May 18 12:00 file1.txt drwxr-xr-x 2 user group 4096 May 17 09:30 folder -rw-r--r-- 1 user group 1234 May 16 15:45 file2.log

Python Script to Convert This into an HTML Table:

python
import datetime # Sample terminal output string (replace this with your command output) terminal_output = """ -rw-r--r-- 1 user group 2345 May 18 12:00 file1.txt drwxr-xr-x 2 user group 4096 May 17 09:30 folder -rw-r--r-- 1 user group 1234 May 16 15:45 file2.log """ # Parse the output into rows lines = [line.strip() for line in terminal_output.strip().split('n') if line.strip()] html_header = """ <html> <head> <style> body { font-family: Arial, sans-serif; background: #f4f4f4; padding: 20px; } table { border-collapse: collapse; width: 100%; background: #fff; } th, td { padding: 8px 12px; border: 1px solid #ddd; text-align: left; } th { background-color: #333; color: white; } tr:nth-child(even) { background-color: #f9f9f9; } </style> <title>Terminal Output Report</title> </head> <body> <h2>Directory Listing Report</h2> <table> <tr><th>Permissions</th><th>Links</th><th>User</th><th>Group</th><th>Size (bytes)</th><th>Date</th><th>Time</th><th>Name</th></tr> """ html_footer = """ </table> </body> </html> """ rows_html = "" for line in lines: # Split by whitespace; note this assumes no spaces in filename parts = line.split() if len(parts) < 9: continue # skip malformed lines permissions, links, user, group = parts[0], parts[1], parts[2], parts[3] size = parts[4] month, day, time_or_year = parts[5], parts[6], parts[7] name = " ".join(parts[8:]) rows_html += f"<tr><td>{permissions}</td><td>{links}</td><td>{user}</td><td>{group}</td><td>{size}</td><td>{month} {day}</td><td>{time_or_year}</td><td>{name}</td></tr>n" html_report = html_header + rows_html + html_footer # Save to a file or print with open("report.html", "w") as f: f.write(html_report) print("HTML report generated: report.html")

Explanation

  • The script reads the terminal output, splits each line into columns.

  • It generates an HTML table with a header row.

  • Basic CSS styles improve readability.

  • The output HTML file can be opened in any web browser.


Extending This Approach

  • Other terminal outputs: Adapt parsing logic for ps, df, top or custom command outputs.

  • Color coding: Add CSS classes to highlight errors or warnings.

  • Charts and graphs: Use JavaScript libraries like Chart.js for statistics.

  • Interactive elements: Add filters, sorting, or collapsible sections.


If you want, I can help you create a script for a specific terminal output or automate the entire process with your actual data. Just share your terminal output sample!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About