79612657

Date: 2025-05-08 14:52:26
Score: 0.5
Natty:
Report link

I managed to fix my code with the help of the first comment, the fix looks like this:
`!for file in data/kapitel*.txt;

do dirname="${file%.*}";

printf 'Creating directory: %s\n' "$dirname" >&2;

mkdir -p "$dirname" && awk -v dir="$dirname" '/^Artikel[[:space:]]/ { close(f);

f = sprintf("%s/Artikel%d.txt", dir, ++n);

printf "Writing to file: %s\n", f | "cat>&2"};

f {print > f}' "$file";done

This is technically one line in my code, I seperated it for some readability. Issue seems to have been

  1. Bad use of

    !ls data/kapitel*.txt \
    | while read file; do
    

    which was replaced by the first line of code
    2. messed up combination of awk and bash

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Leon Preuße