例如,一个文件file1.txt
包含
Hi how are you
hello
today is monday
hello
I am fine
Hi how are you
处理后file1.txt
应该写入file2.txt
,内容应该像这样,而不重复相同的行。
Hi how are you
hello
today is monday
I am fine
我可以使用什么命令在 Linux 终端中执行此操作?
答案1
start cmd:> awk 'lines[$0]++ == 0' input
Hi how are you
hello
today is monday
I am fine
答案2
这是一项简单的工作sort
,使用以下唯一的 ( -u
) 选项sort
:
% sort -u file1.txt
hello
Hi how are you
I am fine
today is monday
要将其保存在file2.txt
:
sort -u file1.txt >file2.txt
如果您想保留初始顺序:
% nl file1.txt | sort -uk2,2 | sort -k1,1n | cut -f2
Hi how are you
hello
today is monday
I am fine