I am trying to sort a file with following command : sort ${filename} > ${filename} , and I get as result - an empty file.
Can someone explain why I get this result and how can I fix it?
Asked
Active
Viewed 166 times
-2
Software_t
- 576
- 3
- 13
2 Answers
1
What you are trying to do is a inplace sort so which may not be supported, try instead.
sort ${filename} > temp_file && mv temp_file ${filename}
RavinderSingh13
- 117,272
- 11
- 49
- 86
1
In short, because Bash truncates the file before sort can read from it. Apart from @RavinderSingh13’s answer, which is the standard way to fix it, you could also use sponge from the moreutils package:
sort ${filename} | sponge ${filename}
Boldewyn
- 78,902
- 44
- 148
- 207