need help please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help please
# 1  
Old 11-13-2011
need help please

So I have a existing file that I used the uniq command on and I need to save the output to the same file without changing the file name.

I have tried $ uniq filename > filename

then when I cat the file it then becomes blank like there is nothing inside.

any help would be much appreciated thanks.
# 2  
Old 11-13-2011
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:
Code:
(uniq filename >$$.foo && mv $$.foo filename) || rm $$.foo

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.

It is the same as:
Code:
if unique filename >$$.foo
then
   mv $$.foo filename
else
   rm $$.foo
fi

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question