Computing.Net > Forums > Unix > Grouping data from a file

Grouping data from a file

Reply to Message Icon

Original Message
Name: bhubb
Date: April 9, 2004 at 06:09:39 Pacific
Subject: Grouping data from a file
OS: hp 11-ux
CPU/Ram: 8/900
Comment:

What UNIX command can I use to get this output:

Smith 3
Jones 1
Arnold 2

from a file with these contents

Smith
Jones
Smith
Arnold
Smith
Arnold

Any help or suggestions would be greatly appreciated...

Bill


Report Offensive Message For Removal


Response Number 1
Name: Castelior
Date: April 9, 2004 at 06:21:56 Pacific
Subject: Grouping data from a file
Reply: (edit)

for i in `cat fic|sort|uniq`
do
name=$i
number=`grep $i fic|wc -w`
echo "$name $number"
done


Report Offensive Follow Up For Removal

Response Number 2
Name: aigles
Date: April 9, 2004 at 06:22:58 Pacific
Subject: Grouping data from a file
Reply: (edit)

The following pipe :

sort input_file | uniq -c

gives :

2 Arnold
1 Jones
3 Smith

If you really want the counter in last position :

sort input_file | uniq -c | awk '{print $2,$1}'

gives

Arnold 2
Jones 1
Smith 3


If you want you can also do all the work with awk (i prefer sort|uniq) :

awk '
{ line[$0]++ }
END { for (l in line) print l,line[l] }
' input_file


Jean-Pierre.


Report Offensive Follow Up For Removal

Response Number 3
Name: Castelior
Date: April 9, 2004 at 06:50:48 Pacific
Subject: Grouping data from a file
Reply: (edit)

I did not know "uniq -c"

think jp


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Grouping data from a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software