The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




Thread: sort
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 01-27-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
There can be some very simple solution to this problem, BUT using awk, this is how we can achieve this


Code:
$ cat file.txt
aaa
b
b
c
c
b
b

I am adding a second field to all lines of file.txt as "1", then using associative array.

$ awk '{print $0,1}' file.txt | awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}'
aaa 1
b 4
c 2
//Jadu