how to save changes to a file

is i want to sort or delete lines etc etc to a file, how do i save the changes.

example i do sort file > file
when i do cat file, all data is erased.
thanks.

Output to a temporary file, and then move it over top of the original file. The trick is to not overlay the original file if the command fails.

#!/usr/bin/env ksh

# straight forward:
if sort file >file.tmp
then
    mv file.tmp file
fi


# less than straight forward, but same effect
sort file >file.tmp && mv file.tmp file

thank you.