You may read alll lines in to array.
before write lines one by one in file whith verification on characters "THis is Linux".
If line finded we befor this line writing "hello world". After append to file next lines.
May be this? Or i not understand you...