nauniq - Non-adjacent uniq


This document describes version 0.110 of nauniq (from Perl distribution App-nauniq), released on 2022-06-02.


 nauniq [OPTION]... [INPUT [OUTPUT]]


nauniq is similar to the Unix command uniq but detects repeated lines even if they are not adjacent. To do this, nauniq must remember the lines being fed to it. It's basically a glorified form of something like these:

 % awk '!mem[$0]++' INPUT
 % perl -ne'print unless $mem{$_}++' INPUT

There are options to control memory usage: option to only remember a certain number of unique lines, option to remember a certain number of characters for each line, and option to only remember the MD5 hash (instead of the content) of each line. There are also other options like append, forget, and so on.


0 on success.

255 on I/O error.

99 on command-line options error.


  • --repeated, -d

    Print only duplicate lines. The opposite of --unique.

  • --ignore-case, -i

    Ignore case.

  • --num-entries=N

    Number of unique entries to remember. The default is -1 (unlimited). This option is to control memory usage, but the consequence is that lines that are too far apart will be forgotten.

  • --skip-chars=N, -s

    Number of characters from the beginning of line to skip when checking uniqueness.

  • --unique, -u

    Print only unique lines. This is the default. The opposite of --repeated.

  • --check-chars=N, -w

    The amount of characters to check for uniqueness. The default is -1 (check all characters in a line).

  • --append

    Open output file in append mode. See also -a.

  • -a

    Equivalent to --append --read-output.

  • --forget-pattern=S

    This is an alternative to --num-entries. Instead of instructing nauniq to remember only a fixed number of entries, you can specify a regex pattern to trigger the forgetting the lines. An example use-case of this is when you have a file like this:

     * entries for 2014-03-13
     * entries for 2014-03-14

    and you want unique lines for each day (in which you'll specify --forget-pattern '^\*').

  • --md5

    Remember the MD5 hash instead of the actual characters of the line. Might be useful to reduce memory usage if the lines are long.

  • --read-output

    Whether to read output file first. This option works only with --append and is usually used via -a to append lines to file if they do not exist yet in the file.


How do I append lines to a file only if they do not exist in the file?

You cannot do this with uniq:

 % ( cat FILE ; produce-lines ) | uniq - FILE
 % ( cat FILE ; produce-lines ) | uniq >> FILE

as it will clobber the file first. But you can do this with nauniq:

 % produce-lines | nauniq -a - FILE


Please visit the project's homepage at


Source repository is at




perlancar <>


To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.


This software is copyright (c) 2022, 2017, 2015, 2014 by perlancar <>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.