How to sort a file in-place

后端 未结 6 1832
青春惊慌失措
青春惊慌失措 2020-11-30 17:37

When we use sort file command, the file shows its contents in a sorted way what if I don\'t want to get any-kind of output but a sorted file?

6条回答
  •  难免孤独
    2020-11-30 18:23

    You can use file redirection to redirected the sorted output:

    sort input-file > output_file
    

    Or you can use the -o, --output=FILE option of sort to indicate the same input and output file:

    sort -o file file
    

    ⚠️ Note: A common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.

提交回复
热议问题