Search code examples
csvuniquedelimiter

How to count number of unique values of a field in a pipe-delimited text file?


I have a pipe-delimited csv file (can be made a .txt file if needed). I want a list of all unique values in column 4. I'm using a Mac with Terminal. Thanks

Sample:

12345|1|2|Blue|54321
23456|1|2|Blue|23456
34567|1|2|Green|34567

I'd like a list that includes 'Blue','Green'


Solution

  • With macOS's built-in awk like this:

    awk -F'|' '{print $4}' YourFile | sort | uniq
    

    Output

    Blue
    Green
    

    Your question title implies you expect the answer to be 2, because there are two unique values, in that case, count the lines too:

    awk -F'|' '{print $4}' file | sort | uniq | wc -l
    2