Skip to content

Instantly share code, notes, and snippets.

@job-gordon
Created October 31, 2025 13:40
Show Gist options
  • Select an option

  • Save job-gordon/5adb241b27e03e8b6e37d8b7e682a9f7 to your computer and use it in GitHub Desktop.

Select an option

Save job-gordon/5adb241b27e03e8b6e37d8b7e682a9f7 to your computer and use it in GitHub Desktop.
another version of converter
#!/bin/bash
# Associative arrays (for keys with spaces) require Bash 4.0+
if [[ -z "${BASH_VERSINFO[0]}" || "${BASH_VERSINFO[0]}" -lt 4 ]]; then
echo "Error: This script requires Bash version 4.0 or higher." >&2
echo "You are running: $BASH_VERSION" >&2
exit 1
fi
input_file=""
output_type=""
output_file=""
# Define the headers in their required order, using the exact names.
headers=("VM ID" "VM Name" "Disk Size" "CPU cores" "Memory (MB)" "Root Password" "User details" "IPv6 Address" "NAT IPv4" "NAT ports" "IPv6 SSH" "NAT SSH" "VNC")
usage() {
echo "Usage: $0 -i inputfilename -t <csv|markdown|html> [-o outputfile]"
echo " -i Optional input file (default: read from standard input)"
echo " -o Optional output file (default: print to console)"
echo " -t Output type (csv, markdown, or html)"
exit 1
}
parse_input() {
while IFS= read -r line; do
# Skip empty lines or lines without a colon
if [[ -z "$line" || "$line" != *":"* ]]; then
continue
fi
# Extract key: everything before the first colon
key="${line%%:*}"
# Extract value: everything after the first colon
value="${line#*:}"
# --- Strip leading/trailing whitespace from key ---
key="${key#"${key%%[![:space:]]*}"}"
key="${key%"${key##*[![:space:]]}"}"
# --- Strip leading/trailing whitespace from value ---
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
# Store in the array
if [ -n "$key" ]; then
vm_data["$key"]="$value"
fi
done
}
# Function to generate CSV output
generate_csv() {
# 1. Print Header
(IFS=,; echo "${headers[*]}")
# 2. Build data row
local data_row=()
for header in "${headers[@]}"; do
# Append the value (or empty string if key doesn't exist)
data_row+=("${vm_data[$header]}")
done
# 3. Print data row
(IFS=,; echo "${data_row[*]}")
}
# Function to generate Markdown table output
generate_markdown() {
# 1. Print Header
printf "|"
for header in "${headers[@]}"; do
printf " %s |" "$header"
done
printf "\n"
# 2. Print Separator
printf "|"
for header in "${headers[@]}"; do
# Create a separator based on header length
printf " %s |" "$(seq -s'-' 1 ${#header} | tr -d '0-9')"
done
printf "\n"
# 3. Print Data
printf "|"
for header in "${headers[@]}"; do
# Print the value, or an empty space if it doesn't exist
local value="${vm_data[$header]}"
local escaped_value="${value//:/\\:}"
printf " %s |" "$escaped_value"
done
printf "\n"
}
# Function to generate HTML table output
generate_html() {
echo "<table>"
# 1. Header Row
echo " <thead>"
echo " <tr>"
for header in "${headers[@]}"; do
echo " <th>$header</th>"
done
echo " </tr>"
echo " </thead>"
# 2. Body Row
echo " <tbody>"
echo " <tr>"
for header in "${headers[@]}"; do
local value="${vm_data[$header]}"
# Use &nbsp; for empty cells to maintain table structure
if [ -z "$value" ]; then
echo " <td>&nbsp;</td>"
else
echo " <td>$value</td>"
fi
done
echo " </tr>"
echo " </tbody>"
echo "</table>"
}
# --- Argument Parsing ---
while getopts "i:t:o:" opt; do
case $opt in
i) input_file="$OPTARG" ;;
t) output_type="$OPTARG" ;;
o) output_file="$OPTARG" ;;
\?) usage ;;
esac
done
# --- Validation ---
if [ -n "$input_file" ] && [ ! -r "$input_file" ]; then
echo "Error: Input file (-i) is missing or not readable." >&2
usage
fi
if [[ "$output_type" != "csv" && "$output_type" != "markdown" && "$output_type" != "html" ]]; then
echo "Error: Invalid output type (-t). Must be 'csv', 'markdown', or 'html'." >&2
usage
fi
# --- Main Logic ---
# 1. Parse the input file into an associative array
# This handles missing fields and whitespace automatically.
declare -A vm_data
if [ -n "$input_file" ]; then
# Read from the specified file
parse_input < "$input_file"
else
# Read from standard input (pipe)
parse_input
fi
# 2. Set up output redirection
if [ -n "$output_file" ]; then
exec > "$output_file"
fi
# 3. Call the correct generation function
case "$output_type" in
csv)
generate_csv
;;
markdown)
generate_markdown
;;
html)
generate_html
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment