You need to write the output to a tmp file and then move it "over' the original file. You also want to ensure that the uniq command was successful before overlaying the bad/empty file on top of the original. Something like this is the one liner form:
This creates the tmp file $$.foo and if the command is successful moves it to replace filename. If the unique command fails the tmp file is removed.