I would extract the first lines into a method "get exceptoin to sed" or similar