Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK - Counting 3 columns, 1000;s records

Status
Not open for further replies.
Feb 12, 2002
80
NO
Ok - I am an AWK novice, and can just about use single line commands to do what I need, but I have now come up against a challenge and I don't have tme to learn AWK to work out how to do it. I'm hoping someone here can help me out.

I have a file that is composed of ~500,000 rows and 3 columns, that looks a bit like this:

MAKE | MODEL | COLOUR
--------------------------
Ford | Mondeo | Red
Ford | Mondeo | Red
Ford | Mondeo | Red
Ford | Mondeo | Red
Ford | Mondeo | Red
Ford | Mondeo | Red
Ford | Mondeo | Red
BMW | 3-Series | Silver
BMW | 3-Series | Silver
BMW | 3-Series | Silver
BMW | 3-Series | Silver
BMW | 3-Series | Silver
Ford | Mondeo | Red
Ford | Mondeo | Red
Ford | Mondeo | Red
BMW | 3-Series | Yellow
BMW | 3-Series | Yellow
BMW | 3-Series | Yellow
BMW | 3-Series | Yellow
Ford | Mondeo | Blue
Ford | Mondeo | Blue
Ford | Mondeo | Blue
Ford | Mondeo | Blue
Ford | Escort | Black
Ford | Escort | Black
Ford | Escort | Silver
Ford | Escort | Silver
Ford | Escort | Silver
Ford | Escort | Silver
Ford | Escort | Black
Ford | Escort | Black
...

Obviously the data here is made up, but hopefully you get the idea.

I want to produce the following:

MAKE | MODEL | COLOUR | Number
Ford | Mondeo | Red | 3
BMW | 3 Series | Silver | 15
...


I know that I have the following unique number of each column:

Make : 518
Model : 92
Colour : 7

Total number of records: 416,098

How can I get the results I require?

All help appreciated.

Thanks.
 
I'm just trying that suggestion ...

I should also say that the pipes are not actually in the file - I added them to my post to try to make it look clearer on screen. The fields are only separted by spaces.

Alignment does not matter, so long as there is separation - the output will hopefully end up in some other Windows based presentation - Excel no doubt.

 
Excellent - that worked a treat:

awk ' { a[$0]++ } END { for (i in a ) print i " " a } ' input.txt > output.txt


That would have taken me many hours to figure out, and would never have worked so well - perfect.

Thank you so much! feherke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top