Created
May 15, 2025 00:36
-
-
Save thushan/e3b1ee731ea1c03eb0a6464b1cd3b94f to your computer and use it in GitHub Desktop.
SQL Server Errors to Markdown Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # License: MIT | |
| # Author: Thushan Fernando <thushan.fernando@gmail.com> | |
| # http://github.com/thushan/ | |
| # | |
| # USAGE: | |
| # ./sql-server-errors-to-markdown.sh errors.txt | |
| set -euo pipefail | |
| INPUT_FILE=$1 | |
| OUTPUT_FILE="dump/sql-errors-$(date +%Y%m%d_%H%M%S).md" | |
| if [[ $# -ne 1 ]]; then | |
| echo "Usage: $0 <error_file>" >&2 | |
| exit 1 | |
| fi | |
| if [[ ! -f $INPUT_FILE ]]; then | |
| echo "Error: File '$INPUT_FILE' not found." >&2 | |
| exit 1 | |
| fi | |
| { | |
| printf '| Type | Element | Description |\n' | |
| printf '|------|---------|-------------|\n' | |
| awk -F': ' ' | |
| /^Error / { | |
| # reconstruct full description | |
| desc_raw = $3 | |
| for (i = 4; i <= NF; i++) desc_raw = desc_raw ": " $i | |
| # extract Type (prefix up to first colon) and rest of description | |
| split(desc_raw, parts, ": ") | |
| type = parts[1] | |
| desc = substr(desc_raw, length(type) + 3) | |
| # clean up Element | |
| elem = $2 | |
| sub(/^Error validating element /, "", elem) | |
| gsub(/\|/, "\\|", elem) | |
| elem = "`" elem "`" | |
| # escape pipes in desc | |
| gsub(/\|/, "\\|", desc) | |
| # output row | |
| printf "| %s | %s | %s |\n", type, elem, desc | |
| } | |
| ' "$INPUT_FILE" | |
| } | sed -E 's/(\[[^]]+\](\.\[[^]]+\])*)/`&`/g' > "$OUTPUT_FILE" | |
| echo "Saved to '$OUTPUT_FILE' :-)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment