shell-scripte-code/convert_to_markdown-bash.sh

37 lines
915 B
Bash

#!/bin/bash
INPUT_FILE="input.txt"
OUTPUT_FILE="output.md"
# Schritt 1: Tabs durch 4 Leerzeichen ersetzen (ohne expand)
sed 's/\t/ /g' "$INPUT_FILE" > temp_processed.txt
# Schritt 2: Hierarchische Listen generieren
while IFS= read -r line; do
# Zähle führende Leerzeichen
spaces=$(echo "$line" | grep -o '^ *' | wc -c)
spaces=$((spaces - 1)) # wc -c zählt Nullbyte
# Berechne Ebene
level=$((spaces / 4))
# Generiere Einrückung
indent=""
for ((i=0; i<level; i++)); do
indent+=" "
done
# Konstruiere neue Zeile
if (( level > 0 )); then
# Entferne originale Einrückung und füge MD-Formatierung hinzu
clean_line="${line:$spaces}"
new_line="${indent}- ${clean_line}"
else
new_line="$line"
fi
echo "$new_line"
done < temp_processed.txt > "$OUTPUT_FILE"
# Aufräumen
rm temp_processed.txt