Split file based on a pattern with leading zeros

view full story

http://unix.stackexchange.com – 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. (HowTos)