How to delete duplicate lines in a file…AWK, SED,

2019-05-18 14:23发布

I find many ways to do this, AWK, SED, UNIQ, but none of them are working on my file.

I want to delete duplicate lines. Here is an example of part of my file:

KTBX
KFSO
KCLK
KTBX
KFSO
KCLK
PAJZ
PAJZ

NOTE: I had to manually add line feeds when I cut and pasted from the file...for some reason it was putting all the variables on one line. Makes me think that my 44,000 line text file actually has only "1" line? Is there a way to modify it so I can delete dups?

2条回答
迷人小祖宗
2楼-- · 2019-05-18 15:07

Perl One-Liner:

perl -nle 'unless($hash{$_}++){print $_}' file

查看更多
劳资没心,怎么记你
3楼-- · 2019-05-18 15:11

You can see all non-printed characters with this command:

od -c oldfile

If all your records are on one line, you can use sed to replace a whitespace (space, tab, newline) with a linebreak:

sed -e 's/\s\+/\n/g' oldfile > oldfile.1

Once you have multiple lines, this awk one-liner:

awk '!x[$0]++' oldfile.1 > newfile

my outfile:

KTBX
KFSO
KCLK
PAJZ
查看更多
登录 后发表回答