How can you find unique lines or duplicate lines in a file based on the content of the first field, or the first n characters ?

The uniq command on OS X lacks the -w switch that is available under Linux: -w, --check-chars=N : compare no more than N characters in lines, which solves the problem.

Can it be done without reimplementing uniq in awk ?

The number of fields is not constant. I would like to use only default tools installed on OS X. I use Macports a lot, but in this case, I don't want to use them.


How about using sort -u? This can at least sort by the first field:

 sort -k 1,1 -u input_file
  • I used this, and defered the treatement of unique or duplicate lines a bit later with a: while read firstField otherFields; do stuffs..
    – alecail
    Oct 1 '12 at 10:31

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.