How to sort a file in-place

后端 未结 6 1823
青春惊慌失措
青春惊慌失措 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:21

    To sort file in place, try:

    echo "$(sort your_file)" > your_file
    

    As explained in other answers, you cannot directly redirect the output back to the input file. But you can evaluate the sort command first and then redirect it back to the original file. In this way you can implement in-place sort.

    Similarly, you can also apply this trick to other command like paste to implement row-wise appending.

提交回复
热议问题