How to remove duplicates from a file and write to the same file?

前端 未结 5 2250
花落未央
花落未央 2021-02-20 16:58

I know my title is not much self-explanatory but let me try to explain it here.

I have a file name test.txt which has some duplicate lines. Now, what I want

5条回答
  •  不要未来只要你来
    2021-02-20 17:07

    Redirection in the shell will not work as you are trying to read and write from the same file at the same time. Actually the file is opened for writing (> file.txt) before the sort is even executed

    @potong's answer works because the sort program itself probably stores all lines in memory, I would not rely on it because it does not explicitly specifies in the manpage that it CAN be the same as the input file (though it will likely work). Unless documented to work "in place" I would not do it (@perreal's answer would work, or you can store intermediate results in shell memory)

提交回复
热议问题