I have a file temp.txt, that I want to sort with the sort
command in bash.
I want the sorted results to replace the original file.
This doesn't work for example (I get an empty file):
sortx temp.txt > temp.txt
Can this be done in one line without resorting to copying to temporary files?
EDIT: The -o
option is very cool for sort
. I used sort
in my question as an example. I run into the same problem with other commands:
uniq temp.txt > temp.txt.
Is there a better general solution?
Many have mentioned the -o option. Here is the man page part.
From the man page:
Read up on the non-interactive editor,
ex
.Use the argument
--output=
or-o
Just tried on FreeBSD:
Another solution:
If you insist on using the
sort
program, you have to use a intermediate file -- I don't thinksort
has an option for sorting in memory. Any other trick with stdin/stdout will fail unless you can guarantee that the buffer size for sort's stdin is big enough to fit the entire file.Edit: shame on me.
sort temp.txt -o temp.txt
works excellent.