0

I have a file in Unix like this

name1 text text 123432re text
name2 text text 12344qp text
name3 text text1 134234ts text
name3 text text2 134234ts text

I want to find all the different types of values in the 3rd column for all user names, lets say name1, name2 and name3.

Like below:

name1 1
name2 1
name3 2

How can I get the required result?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

1

If the text in the columns before the 4th column cannot contain spaces, the following should do it with gawk:

gawk '{++vals[$1][$3];}
  END {for (u in vals) {
       c = 0;
       for (t in vals[u]) { ++c; };
       print u" "c;}
}' yourfile

(Note, gawk supports multidimensional arrays, while standard awk doesn't, so the same solution won't work with standard awk.)

lp_
  • 1,158
  • 1
  • 14
  • 21
  • Useless use of cat award! Also, awk doesn't support 2D arrays, so this won't work. – Tom Fenech Apr 22 '15 at 16:14
  • @Tom Fenech Thanks, my mistake. I haven't noticed that the question was marked only as a standard awk question. Still, a gawk solution might be helpful too. – lp_ Apr 22 '15 at 16:23
  • @lp_ Thank you very much. Your gawk has solved my requirement. Once again thank you very very much – user3744631 Apr 23 '15 at 10:30