如何将文件内容写入新文件并删除重复行

Nav*_* Cp 3 linux command-line vi files

例如,一个文件file1.txt包含

Hi how are you  
hello  
today is monday  
hello  
I am fine  
Hi how are you 
Run Code Online (Sandbox Code Playgroud)

处理后file1.txt应该写入file2.txt,内容应该是这样的,不要重复相同的行。

Hi how are you  
hello  
today is monday  
I am fine  
Run Code Online (Sandbox Code Playgroud)

我可以使用什么命令在 linux 终端中执行此操作?

Hau*_*ing 6

start cmd:> awk 'lines[$0]++ == 0' input
Hi how are you
hello  
today is monday  
I am fine
Run Code Online (Sandbox Code Playgroud)


hee*_*ayl 5

这是一个简单的工作sort,使用独特的 ( -u) 选项sort

% sort -u file1.txt
hello
Hi how are you
I am fine
today is monday
Run Code Online (Sandbox Code Playgroud)

将其保存在file2.txt

sort -u file1.txt >file2.txt
Run Code Online (Sandbox Code Playgroud)

如果要保留初始顺序:

% nl file1.txt | sort -uk2,2 | sort -k1,1n | cut -f2
Hi how are you
hello
today is monday
I am fine
Run Code Online (Sandbox Code Playgroud)