I've just run the following in bash:

uniq .bash_history > .bash_history

and my history file ended up completely empty.

I guess I need a way to read the whole file before writing to it. How is that done?

PS: I obviously thought of using a temporary file, but I'm looking for a more elegant solution.

link|improve this question
It's because the files get opened from right to left. See also stackoverflow.com/questions/146435/… – kaerast Apr 24 '10 at 18:38
feedback

7 Answers

up vote 6 down vote accepted

I recommend using sponge from moreutils. From the manpage:

DESCRIPTION
  sponge  reads  standard  input  and writes it out to the specified file. Unlike
  a shell redirect, sponge soaks up all its input before opening the output file.
  This allows for constructing pipelines that read from and write to the same 
  file.

To apply this to your problem, try:

uniq .bash_history | sponge .bash_history
link|improve this answer
It's like cat, but with sucking capabilities :D – MilliaLover Apr 28 '10 at 0:11
feedback

use sponge from moreutils

uniq .bash_history | sponge .bash_history
link|improve this answer
feedback

The problem is that your shell is setting up the command pipeline before running the commands. It's not a matter of "input and output", it's that the file's content is already gone before uniq even runs. It goes something like:

  1. The shell opens the > output file for writing, truncating it
  2. The shell sets up to have file-descriptor 1 (for stdout) be used for that output
  3. The shell executes uniq, perhaps something like execlp("uniq", "uniq", ".bash_history", NULL)
  4. uniq runs, opens .bash_history and finds nothing there

There are various solutions, including the in-place editing and the temporary file usage which others mention, but the key is to understand the problem, what's actually going wrong and why.

link|improve this answer
feedback

This sed script removes adjacent duplicates. With the -i option, it does the modification in-place. It's from the sed info file:

sed -i 'h;:b;$b;N;/^\(.*\)\n\1$/ {g;bb};$b;P;D' .bash_history
link|improve this answer
sed still uses the temp file, added an answer with strace illustration (not that it really matters) :-) – Kyle Brandt Apr 25 '10 at 1:15
@Kyle: True, but "out of sight, out of mind". Personally, I would use the explicit temporary file since something like process input > tmp && mv tmp input is much simpler and more readable than using sed trickery simply to avoid a temp file and it won't overwrite my original if it fails (I don't know if sed -i fails gracefully - I would think it would though). Besides, there are lots of things you can do with the output-to-temp-file method that can't be done in-place without something even more involved than this sed script. I know you know all this, but it may benefit some onlooker. – Dennis Williamson Apr 25 '10 at 2:54
feedback

As an interesting tidbit, sed uses a temp file as well (this just does it for you):

$ strace sed -i 's/foo/bar/g' foo    
open("foo", O_RDONLY|O_LARGEFILE)       = 3
...
open("./sedPmPv9z", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600) = 4
...
read(3, "foo\n"..., 4096)               = 4
write(4, "bar\n"..., 4)                 = 4
read(3, ""..., 4096)                    = 0
close(3)                                = 0
close(4)                                = 0
rename("./sedPmPv9z", "foo")            = 0
close(1)                                = 0
close(2)                                = 0

Description:
The tempfile ./sedPmPv9z becomes fd 4, and the foo files becomes fd 3. The read operations are on fd 3, and the writes on fd 4 (the temp file). The foo file is then overwritten with the temp file in the rename call.

link|improve this answer
feedback

A temporary file is pretty much it, unless the command in question happens to support in place editing (uniq doesn;t - some seds do (sed -i)).

link|improve this answer
feedback

if you're looking for a one-liner, how about this:

uniq .bash_history | cat > .bash_history

By the way, you have to use sort before uniq.

Best regards,

Martin

link|improve this answer
I just want to remove adjacent duplicates – MilliaLover Apr 24 '10 at 18:57
That command made my history file zero-sized too – MilliaLover Apr 24 '10 at 19:20
1  
Useless use of cat, this does exactly the same thing as the OP's command. – James Apr 24 '10 at 21:38
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.