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

前端 未结 5 2229
花落未央
花落未央 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:24

    Use Sponge for Reading/Writing to Same File

    You can use the sponge utility from moreutils to soak up standard output before writing the file. This prevents you from having to shuffle files around, and approximates an in-place edit. For example:

    sort -u test.txt | sponge test.txt
    

    Sample Output

    Using your corpus, this results in the expected output.

    $ cat test.txt 
    AAAA
    BBBB
    CCCC
    

提交回复
热议问题