by CraigJPerry · 12y ago · 0 comments
You need to be a little careful with that. If you do:
uniq -u movies.csv > movies.csv
The shell will first open movies.csv for writing (the redirect part) then launch the uniq command connecting stdout to the now emptied movies.csv.Of course when uniq opens movies.csv for consumption, it'll already be empty. There will be no work to do.
There's a couple of options to deal with this, but the temporary intermediate file is my preference provided there's sufficient space - it's easily understood, if someone else comes across the construct in your script, they'll grok it.
http://linux.die.net/man/1/sponge
uniq -u movies.csv | sponge movies.csvuniq -u movies.csv > temp.csv
temp.csv > movie.csv
rm temp.csv
The rename is atomic; anyone opening "filename" will get either the old version, or the new version. (Although it breaks one of my other favorite idioms for monitoring log files, "tail -f filename", because the old inode will never be updated.)
You should look into the '-F' option of tail; it follows the filename, and not the inode.
uniq -u movies.csv > /tmp/temp.csv && mv /temp/temp.csv movies.csv
$ temp.csv > movie.csv
temp.csv: command not found