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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging multiple uniq -c output files 1

Status
Not open for further replies.

zenetko

Programmer
Dec 22, 2001
6
AE
I've got multiple files which have sorted uniq -c outputs. The 1st field is a number and the second field is some name. Now I need to merge such outputs from a number of files. A name could occur more than once accross these files but the 1st field would differ. I need to add up the 1st field (which is a number) of similar names occuring in a number of files to produce a master file. Any ideas ?
 
Ivoke this supplying it with multiple files.

nawk '{arr[$2]+=$1} END{ for (i in arr) print i, arr}' AllMyFilesGoHere

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks that did the job. I was trying to understand arr[$2]+=$1 part... how is awk handling it? I'm guessing that all the rows from all the files are being loaded to memory and the matching $2 fields are being poped to add their 1st field.

Been working with basic awk since a while. You recommend any good reading materials online or some books to refer to regarding awk ?

Thanks Again

 
Correct. It's using associative arrays where arrays are index by string values. The array is being index by 'name' and it keeps adding values for the same names. You end up with with an array holding totals for a given 'name'.

For the awk pointers I'd start with:


Or/and getting an O'Reilly &quot;sed and awk&quot;:
By Dale Dougherty & Arnold Robbins; ISBN 1-56592-225-5, 432 pages

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If you are working in an environment that is capable
and willing to go with the best (features wise) awk,
I'd suggest gawk.

Network programming, improved control and a lot
of design tweaks make it a very nice tool to work
with if you are used to an older awk or nawk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top