Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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