如何从文件中删除重复写同一个文件? [英] How to remove duplicates from a file and write to the same file?

查看:134
本文介绍了如何从文件中删除重复写同一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的标题是不是不言自明,但让我尝试在这里解释一下。

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

我有一个文件名的test.txt 有一些重复线路。现在,我想要做的就是消除这些重复行,并在同一时间更新的test.txt 通过新的内容。

I have a file name test.txt which has some duplicate lines. Now, what I want to do is remove those duplicate lines and at the same time update test.txt with the new content.

的test.txt

AAAA
BBBB
AAAA
CCCC

我知道我可以使用排序-u的test.txt 来删除重复的,但以更新新的内容我怎么重定向其输出到同一个文件的文件。下面的命令不起作用。

I know I can use sort -u test.txt to remove the duplicates but to update the file with new content how do I redirect it's output to the same file. The below command doesn't work.

sort -u test.txt > test.txt

那么,为什么上面的命令没有工作,什么正确的方法是什么?

So, why the above command is not working and whats the correct way?

也有像任何其他方式

sort_and_update_file test.txt

进行排序,并自动

更新我的文件无需任何重定向。

which sorts and automatically updates my file without any need of redirection.

推荐答案

这可能会为你工作:

sort -u -o test.txt test.txt

这篇关于如何从文件中删除重复写同一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆