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
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