in reply to Replace first blank line in a text file with headers.
Your if statement appears to match the beginning of a line followed by none white space (^\S). Did you mean ^\s* ? or the more accurate \A\s*\Z.
Neil Watson watson-wilson.ca