如何在unix bash中删除文本文件中的重复行?

t28*_*292 7 bash

我只有一个包含多行的file.txt,我想删除重复的行而不对文件进行排序.我可以在unix bash中使用什么命令?

file.txt的样本

orangejuice;orange;juice_apple
pineapplejuice;pineapple;juice_pineapple
orangejuice;orange;juice_apple
Run Code Online (Sandbox Code Playgroud)

输出样本:

orangejuice;orange;juice_apple
pineapplejuice;pineapple;juice_pineapple
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 35

一种方式使用awk:

awk '!a[$0]++' file.txt
Run Code Online (Sandbox Code Playgroud)


cho*_*oba 14

您可以使用Perl:

perl -ne 'print unless $seen{$_}++' file.txt
Run Code Online (Sandbox Code Playgroud)

-n开关使Perl逐行处理文件.每行($_)都作为键存储在名为"see"的哈希中,但是++在返回值后发生,第一次遇到该行时会打印该行.