Linux

如何將文件內容寫入新文件刪除重複行

  • January 25, 2016

例如,一個文件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 終端中使用什麼命令來執行此操作?

這是一項簡單的工作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
start cmd:> awk 'lines[$0]++ == 0' input
Hi how are you
hello  
today is monday  
I am fine

引用自:https://unix.stackexchange.com/questions/257454