Hacker News new | past | comments | ask | show | jobs | submit login

If you do something like:

  echo "hello world" >> dummyfile
  cat dummyfile | sed 's/hello/goodbye/' > dummyfile
dummyfile will be blank afterwards

if you do:

  cat dummyfile | sed 's/hello/goodbye/' | sponge dummyfile
It'll work as you'd expect (or at least as I'd expect). Otherwise `> dummyfile` will truncate dummyfile before it's read and inputed into sed.



For the sed example, what you actually want is -i which modifies the given file.

AKA

sed -i 's/hello/goodby/' dummyfile


That's only in gnu coreutils. BSD coreutils don't support -i



Ah, yes. I like it. I've definitely made the exact mistake you point out. Thanks!


I actually think I learned about sponge after I burned myself with this same mistake.


Isn't that what `tee` does, minus the "also displaying on STDOUT" bit?


tee doesn't store all the input before writing as far as I know, so it wouldn't work either in this case.


Is this true? I though the issue was that the write might happen while the file is still being read?

Does the '>' operator blanks the file first, before the read happens?


> erases the file first and >> appends without erasing

This isn't the issue.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: