Artificial intelligent assistant

Split file based on a pattern with leading zeros I have a book in text format. I would like to split the book into several files where each file contains a single chapter. Therefore I'm using the following command: awk '/Chapter/{i++}{print > "chap"i}' "$f.txt" where $f is just the variable in a for-loop. I notice however that the files are named: chap1 chap9 chap11 Is their a way to add leading zeros? So the files are named: chap001 chap009 chap011 I'm using `/bin/sh` as default shell.

One way:


awk '/Chapter/{i=sprintf("%03d",i+1)}{print > "chap"i}' "$f.txt"

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 3f13911830af7aa4b0a604707aa6dad5