3

Two columns A and B: Column A have repeated ids and column B has different values for each corresponding repeated value. I would like to read every uniq value from Column A with only the first hit from column B

File_B.txt

A 1
A 2
B 3
B 2
B 4
C 1
C 5
D 4
D 3
E 2

Output:

A 1
B 3
C 1
D 4
E 2

2 Answers 2

6
sort -k1,1 -u file

Sort file by first column and take first unique entry only.

1
  • 2
    Note that sort -u can return incorrect result in locales which have collating characters sort the same. Example printf '%b\n%b\n' $'\U2460' $'\U2461' | LC_ALL=en_US.UTF-8 sort -u
    – cuonglm
    Aug 20, 2015 at 15:13
4

With awk:

awk '!($1 in a){a[$1];print}' <file

or:

awk '!a[$1]++' <file
0

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.