Search code examples
bashuniq

How to print only the unique lines in BASH?


How can I print only those lines that appear exactly once in a file? E.g., given this file:

mountain
forest
mountain
eagle

The output would be this, because the line mountain appears twice:

forest
eagle
  • The lines can be sorted, if necessary.

Solution

  • Using awk:

    awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' file
    eagle
    forest