130

A huge (up to 2 GiB) text file of mine contains about 100 exact duplicates of every line in it (useless in my case, as the file is a CSV-like data table).

What I need is to remove all the repetitions while (preferably, but this can be sacrificed for a significant performance boost) maintaining the original sequence order. In the result each line is to be unique. If there were 100 equal lines (usually the duplicates are spread across the file and won't be neighbours) there is to be only one of the kind left.

I have written a program in Scala (consider it Java if you don't know about Scala) to implement this. But maybe there are faster C-written native tools able to do this faster?

UPDATE: the awk '!seen[$0]++' filename solution seemed working just fine for me as long as the files were near 2 GiB or smaller but now as I am to clean-up a 8 GiB file it doesn't work any more. It seems taking infinity on a Mac with 4 GiB RAM and a 64-bit Windows 7 PC with 4 GiB RAM and 6 GiB swap just runs out of memory. And I don't feel enthusiastic about trying it on Linux with 4 GiB RAM given this experience.

  • this will destroy your ordering but,have you tried sort -u, I have no idea how or if it can run on such a massive file – 0x7c0 Jan 27 '12 at 15:57
  • 5
    C is often not significantly faster than Java, and if you're running it (in-order) now, there's a fair chance it'll finish before you get an answer here, implement it, and it finishes running; out of order, sort -u will probably be faster. – Kevin Jan 27 '12 at 15:59
219

An awk solution seen on #bash (Freenode):

awk '!seen[$0]++' filename
  • 1
    Just tried this on a 2G file and it took three minutes on my notebook. Not bad. I also tried uniq filename | awk '!seen[$0]++', but it wasn't any faster. – mgjk Jan 27 '12 at 19:27
  • This is surprisingly faster than a more verbose awk version using 2 array lookups (shown as an expanded explanation in Gilles answer) : 0m36.132s vs 0m49.958s.. for 50 million lines.. I thought the bottleneck would be the I/O, but the extra array lookup is... 1 million elements in the array seems to make a rather significant dent... – Peter.O Jan 28 '12 at 5:53
  • But how does that compare to sort -u .... ? – HashWizard May 13 '17 at 21:38
  • 1
    @HashWizard: this command does not sort, but eliminates every next occurrence of the same line – enzotib May 14 '17 at 15:51
  • 1
    @MaxWilliams yes, it works is they are randomly distributed. – setholopolus Jan 19 '18 at 19:58
47

There's a simple (which is not to say obvious) method using standard utilities which doesn't require a large memory except to run sort, which in most implementations has specific optimizations for huge files (a good external sort algorithm). An advantage of this method is that it only loops over all the lines inside special-purpose utilities, never inside interpreted languages.

<input nl -b a -s : |           # number the lines
sort -t : -k 2 -u |             # sort and uniquify ignoring the line numbers
sort -t : -k 1n |               # sort according to the line numbers
cut -d : -f 2- >output          # remove the line numbers

If all lines begin with a non-whitespace character, you can dispense with some of the options:

<input nl | sort -k 2 -u | sort -k 1n | cut -f 2- >output

For a large amount of duplication, a method that only requires storing a single copy of each line in memory will perform better. With some interpretation overhead, there's a very concise awk script for that (already posted by enzotib):

<input awk '!seen[$0]++'

Less concisely: !seen[$0] {print} {seen[$0] += 1}, i.e. print the current line if it hasn't been seen yet, then increment the seen counter for this line (uninitialized variables or array elements have the numerical value 0).

For long lines, you can save memory by keeping only a non-spoofable checksum (e.g. a cryptographic digest) of each line. For example, using SHA-1, you only need 20 bytes plus a constant overhead per line. But computing digests is rather slow; this method will only win if you have a fast CPU (especially one with a hardware accelerator to compute the digests) and not a lot of memory relative to the size of the file and sufficiently long lines. No basic utility lets you compute a checksum for each line; you'd have to bear the interpretation overhead of Perl/Python/Ruby/… or write a dedicated compiled program.

<input perl -MDigest::MD5 -ne '$seen{Digest::MD5::md5($_)}++ or print' >output
  • @Gilles Based on your explanation of awk '!seen[$0]++', does it mean that if awk sees 2 duplicate lines, it will keep the always first one and ignore all subsequent ones? (Or it will keep the last one?) – user779159 May 3 '17 at 11:12
  • 1
    @user779159 It keeps the first one: each input line is either printed immediately (first occurrence) or not at all (repeat occurrence). – Gilles 'SO- stop being evil' May 3 '17 at 11:30
  • But how does that compare to sort -u ...? – HashWizard May 13 '17 at 21:37
  • @HashWizard A plain sort -u changes the order. My answer shows solutions that preserve the order (the order of first occurrences, to be precise). – Gilles 'SO- stop being evil' May 13 '17 at 21:42
  • @Gilles would you say that it is faster than sort -u for large files (10G) with 50% duplicates ? – HashWizard May 13 '17 at 21:43
25
sort -u big-csv-file.csv > duplicates-removed.csv

Note that the output file will be sorted.

  • 1
    Not as fast as the awk command in other answers, but conceptually simple! – Johann Mar 31 '15 at 23:11
  • @Johann I am doing this pretty often on files with hundreds of thousands (even million) of short newline terminated strings. I get the results pretty quick for the experiments I am doing. It can be more important if used in scripts which are run again and again, savings in time can be considerable. – Vladislavs Dovgalecs Mar 31 '15 at 23:13
  • 1
    Use sort -u to remove duplicates during the sort, rather than after. (And saves memory bandwidth) piping it to another program). This is only better than the awk version if you want your output sorted, too. (The OP on this question wants his original ordering preserved, so this is a good answer for a slightly different use-case.) – Peter Cordes Sep 14 '15 at 15:39
  • Took about a minute, for me, for a 5.5 million line file (1.8 GB in total). Brilliant. – Max Williams Jan 4 '18 at 11:23
18

Assuming you can afford to keep as much as the de-duplicated file in memory (if your data is indeed duplicated by a factor of 100, that should be about 20MiB + overhead), you can do this very easily with Perl.

$ perl -ne 'print unless $dup{$_}++;' input_file > output_file

This preserves the order too.

You could extract the number of occurrences of each line from the %dup hash if you so wished, as an added free bonus.

If you prefer awk, this should do it too (same logic as the perl version, same ordering, same data gathered in the dup variable):

$ awk '{if (++dup[$0] == 1) print $0;}' input_file > output_file
  • This is too good @Mat, I was about to slurp the file, lol ;-). – Nikhil Mulley Jan 27 '12 at 16:10
  • Now waiting for @ManAtWork for his sed and awk magic weavery too :-) – Nikhil Mulley Jan 27 '12 at 16:11
  • awesome again for the awk tip :-) – Nikhil Mulley Jan 27 '12 at 16:18
  • 1
    Is it possible to change the perl script to only remove duplicate adjacent lines? – dumbledad Mar 10 '16 at 0:11
  • 2
    @dumbledad: uniq does that all by itself – Mat Mar 10 '16 at 5:50
4

As no other answer provided inplace support, here is one:

gawk -i inplace '!a[$0]++' file
  • Does this preserve the order? By the way, this did not work for me. My version is: GNU Awk 4.0.2 – Leonid Feb 16 '17 at 10:31
  • 1
    @Leonid yes, it does. It prints the first occurrence of any unique line. The inplace support was first introduced in version 4.1, which was released in 2013. – Jan Chren - rindeal Feb 16 '17 at 12:49
3

You can use uniq http://www.computerhope.com/unix/uuniq.htm

uniq reports or filters out repeated lines in a file.

  • When giving an answer it is preferable to give some explanation as to WHY your answer is the one. So, how does this answer differ from several of the previous answers? – Stephen Rauch Mar 24 '17 at 4:08
  • 2
    From the uniq man page:Note: 'uniq' does not detect repeated lines unless they are adjacent. So you have to first sort it and loose the order of the non duplicate lines. – Vindolin Nov 6 '18 at 7:27
2

Python One liners :

python -c "import sys; lines = sys.stdin.readlines(); print ''.join(sorted(set(lines)))" < InputFile
  • this causes the entire file to be slurped into memory and may not be a good fit for the OP's problem. Also not guaranteed to retain order – iruvar Sep 15 '13 at 14:50
  • Thanks for the suggestion, I've been just learning python.. just tried this for learning purpose.. :) – Rahul Patil Sep 15 '13 at 19:52
  • Here's a Python 2.7 version that is not a one-liner but (succinctly) returns unique lines preserving order without either loading the entire file into memory or creating a single gigantic string to feed to print – iruvar Sep 16 '13 at 16:37
  • Thanks @1_CR I have something learn today :) OrderedDict – Rahul Patil Sep 16 '13 at 16:39
0

None of the answers here worked for me on my Mac so I wrote a simple python script that works for me. I am ignoring leading/trailing whitespace and also don't care about memory consumption.

import sys

inputfile = sys.argv[1]
outputfile = sys.argv[2]

with open(inputfile) as f:
    content = f.readlines()

content = [x.strip() for x in content]

my_list = list(set(content))

with open(outputfile, 'w') as output:
    for item in my_list:
        output.write("%s\n" % item)

Save the above to unique.py and run like this:

python unique.py inputfile.txt outputfile.txt
-1

With bash 4, a pure-bash solution that takes advantage of associative arrays can be used. Here is an example

unset llist; declare -A llist;
while read -r line; do
if [[ ${llist[$line]} ]]; then
  continue
else 
  printf '%s\n' "$line"
  llist[$line]="x"
fi
done < file.txt
  • 2
    Don't use read loops to process big text files. bash has to read one-byte-at-a-time to avoid overshooting a newline. Bash is also not very fast at text processing in general compared to awk. If you do use this, read -ra will avoid eating backslashes in your input. Also, don't forget to unset llist after the loop, if you put this in a shell function or use it interactively. – Peter Cordes Sep 14 '15 at 15:44
  • 2
    @PeterCordes, or you could have just referenced this :-) – iruvar Sep 14 '15 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.