Linux
如何將文件內容寫入新文件刪除重複行
例如,一個文件
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