in reply to Script wont run

Reading from and writing to the same open filehandle is tricky.

It may be better to open the file in read mode, read in the data, close the file, process the data, open again in write mode ( '>' ) and write the whole file anew.

You could also adapt the technique to save a backup copy of the file before you write out the new data.

HTH

The way forward always starts with a minimal test.