Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Distinct Values from Field AWK

I'm looking for a way to print the distinct values in a field while in the command-prompt environment using AWK.

ID     Title     Promotion_ID     Flag
12     Purse       7               Y
24     Wallet      7               Y
709    iPhone      1117            Y
74     Satchel     7               Y
283    Xbox        84              N

Ideally I'd like to return the promotion_ids: 7, 1117, 84. I've researched the question on Google and have found some examples such as:

`cut -f 3 | uniq *filename.ext*`               (returned error)
`awk cut -f 3| uniq *filename.ext*`            (returned error)
`awk cut -d, -f3 *filename.ext* |sort| uniq`   (returned error)
like image 666
Scott Davis Avatar asked Oct 14 '25 14:10

Scott Davis


2 Answers

awk 'NR>1{a[$3]++} END{for(b in a) print b}' file

Output:

7
84
1117
like image 75
Cyrus Avatar answered Oct 17 '25 14:10

Cyrus


with the pipe line

$ sed 1d file    |    # remove header
  tr -s ' ' '\t' |    # normalize space delimiters to tabs
  cut -f3        |    # isolate the field
  sort -nu            # sort numerically and report unique entries

7
84
1117
like image 42
karakfa Avatar answered Oct 17 '25 13:10

karakfa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!