Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line.

So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4 spaces, then put the text after the aforementioned 4 spaces.

I think I'm close, but I've spent a stupid amount of time googling with little luck.

Hi.

Maybe the simplest way is to just remove any whitespace, and put four spaces there?

sed "1s/^ */    /" input_file

To write this change to the same file, use sed -i. If your sed doesn't support the -i option, you can use ed, instead:

ed input_file << !
1,s/^ */    /
w
q
!

Oh hey, that's genius... I blame my lack of such a simple solution on the fact it'd been 8 hours at the screen doing other things already.

Thanks a bunch!