Как удалить дубликаты строк в огромном текстовом файле в пакете - PullRequest
0 голосов
/ 29 сентября 2019

У меня есть текстовый файл размером более 50 ГБ.Он содержит много строк, каждая строка в среднем около 15 символов.Я хочу, чтобы каждая строка была уникальной (с учетом регистра).Поэтому, если строка точно такая же, как и другая, ее необходимо удалить, не меняя порядок других строк и не сортируя файл каким-либо образом.

Мой вопрос отличается от других, поскольку у меня огромныйфайл, который не может быть обработан другими решениями, которые я искал.

Я пробовал:

awk !seen[$0]++ bigtextfile.txt > dublicatesremoved.txt

, он запускается красиво и быстро, но очень скоро я получаю следующую ошибку:

awk: (FILENAME=bigtextfile.txt FNR=19083509) fatal: more_nodes: nextfree: can't allocate 4000 bytes of memory (Not enough space)

Вышеуказанная ошибка появляется, когда размер выходного файла составляет около 200 МБ.

Есть ли другой быстрый способ сделать то же самое в Windows?

1 Ответ

5 голосов
/ 29 сентября 2019

Вы можете сделать это на компьютере UNIX или Cygwin поверх Windows:

$ cat file
Speed, bonnie boat, like a bird on the wing,
Onward! the sailors cry;
Carry the lad that's born to be King
Over the sea to Skye.

Loud the winds howl, loud the waves roar,
Speed, bonnie boat, like a bird on the wing,
Thunderclaps rend the air;
Onward! the sailors cry;
Baffled, our foes stand by the shore,
Carry the lad that's born to be King
Follow they will not dare.
Over the sea to Skye.

.

$ cat -n file | sort -k2 -u | sort -n | cut -f2-
Speed, bonnie boat, like a bird on the wing,
Onward! the sailors cry;
Carry the lad that's born to be King
Over the sea to Skye.

Loud the winds howl, loud the waves roar,
Thunderclaps rend the air;
Baffled, our foes stand by the shore,
Follow they will not dare.

Единственная команда, приведенная выше, пытается обработать весь файл сразу:sort и sort предназначены для использования подкачки и т. Д., Чтобы точно обрабатывать большие файлы (см. https://unix.stackexchange.com/q/279096/133219), так что ИМХО это ваш лучший способ сделать это.

Startс помощью cat -n file, а затем добавляйте каждую команду в конвейер по одной, чтобы увидеть, что она делает (см. ниже), но сначала просто добавьте номера строк, чтобы мы могли затем уникально отсортировать по содержимому, чтобы получить уникальные значения, а затем отсортировать поисходные номера строк, чтобы вернуть исходный порядок строк и затем удалить номера строк, которые мы добавили на первом шаге:

$ cat -n file
     1  Speed, bonnie boat, like a bird on the wing,
     2  Onward! the sailors cry;
     3  Carry the lad that's born to be King
     4  Over the sea to Skye.
     5
     6  Loud the winds howl, loud the waves roar,
     7  Speed, bonnie boat, like a bird on the wing,
     8  Thunderclaps rend the air;
     9  Onward! the sailors cry;
    10  Baffled, our foes stand by the shore,
    11  Carry the lad that's born to be King
    12  Follow they will not dare.
    13  Over the sea to Skye.
    14

.

$ cat -n file | sort -k2 -u
     5
    10  Baffled, our foes stand by the shore,
     3  Carry the lad that's born to be King
    12  Follow they will not dare.
     6  Loud the winds howl, loud the waves roar,
     2  Onward! the sailors cry;
     4  Over the sea to Skye.
     1  Speed, bonnie boat, like a bird on the wing,
     8  Thunderclaps rend the air;

.

$ cat -n file | sort -k2 -u | sort -n
     1  Speed, bonnie boat, like a bird on the wing,
     2  Onward! the sailors cry;
     3  Carry the lad that's born to be King
     4  Over the sea to Skye.
     5
     6  Loud the winds howl, loud the waves roar,
     8  Thunderclaps rend the air;
    10  Baffled, our foes stand by the shore,
    12  Follow they will not dare.

.

$ cat -n file | sort -k2 -u | sort -n | cut -f2-
Speed, bonnie boat, like a bird on the wing,
Onward! the sailors cry;
Carry the lad that's born to be King
Over the sea to Skye.

Loud the winds howl, loud the waves roar,
Thunderclaps rend the air;
Baffled, our foes stand by the shore,
Follow they will not dare.
...