Merging files and increment value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging files and increment value
# 1  
Old 10-05-2010
Computer Merging files and increment value

Hi,

I have number of files which are collected from different machines. Files contains user id and their number of attempts to login. How can I combine these files based on uniq id and add number of attempts together.

For example:

$ cat file1
testid1,3
testid2,1
testid3,5

$ cat file2
testid1,2
testid4,1
testid3,1
testid6,2

$ cat file3
testid1,5
testid4,1
testid6,2

....



Desired output:

$ cat uniq_ids_with_total_login_attempts

testid1,10
testid2,1
testid3,6
testid4,2
testid6,4
.......



I tried my luck with 'join' but didn't work. Do you guys have any idea how it can be done?


Thanks,
# 2  
Old 10-05-2010
If you join-keeping-all from file1 to file2 making file2, file 2x to file3 getting file 3x, you end up with a variable length line like:
key,#,#,#
I have a many to one join with keep-all I wrote that allows pipes (no seeks, file 1 has to be the many and file 2 the one), m1join.c, because I love pipes and hate temp files and step by step.

From there, it is not hard to process it, like with sed into ksh input:
echo "key<tab>$(( # + # + # ))"
# 3  
Old 10-05-2010
Each ID appears only once in the file ?
If so you could create an ID file then use it to load ID to grep in the 3 files
then just sum the values of second field...
# 4  
Old 10-05-2010
Try:
Code:
awk -F, '{a[$1]+=$2}END{for (i in a){print i FS a[i]}}' file*

# 5  
Old 10-05-2010
PERL is good at storing and updating keyed values, too.

---------- Post updated at 10:20 AM ---------- Previous update was at 09:54 AM ----------

If you sort the files together, it is not hard to do a little ksh loop that adds while the id is identical and prints when it changes. Put a dummy id zzzzzzzz into the stream to force the last print.
Code:
(
sort . . . echo zzzzzzzz
)| (
zlast= ztot=0 while read ln do
zid=${ln%%,*} if [ "$zid" != "$zlast" ] then
if [ $ztot != 0 ] then
echo $zlast,$ztot
fi zlast=$zid ztot=${ln##*,}
else
ztot=$(( $ztot + ${ln##*,} ))
fi
done
)


Last edited by Scott; 10-20-2010 at 03:13 PM.. Reason: Added code tags :-o
# 6  
Old 10-05-2010
Thank you all for awesome ideas. Am going to try bartus11 & DGPickett's code on aprox. 100K ids to see which one is faster.


Thanks again.
# 7  
Old 10-05-2010
Code:
$ ruby -F","  -ane 'BEGIN{h={};h.default=0}; h[$F[0].strip]+=$F[1].to_i if $F[0]; END{h.each_pair{|x,y| print "#{x} #{y}\n"}}' file file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

2. Shell Programming and Scripting

Merging two files

Guys, I am having little problem with getting a daily report! The daily process that I do is as follows 1. Unload Header for the report from the systables to one unl file, say Header.unl 2. Unload the data from the required table/tables to another unl file, say Data.unl 3. Send a... (2 Replies)
Discussion started by: PikK45
2 Replies

3. Shell Programming and Scripting

merging two files

file1.txt 1 2 10 11 56 57 7 8 43 44 and let's suppose that there is a file called file2.txt with 100 columns I want to produce a file3.txt with columns specified in file1.txt in that order (1,2,10,11,56,57,7,8,43,44) Thanks! (2 Replies)
Discussion started by: johnkim0806
2 Replies

4. Shell Programming and Scripting

Merging files

I have two files file 1 containing x rows and 1 column file 2 containing x rows and 1 column I want to merge both the files and add a comma between the two eg plz guide (1 Reply)
Discussion started by: test_user
1 Replies

5. Shell Programming and Scripting

Copying files with increment in destination

Hi.. i have a source and destination directories & want to copy files from source to destination those files are unique to source directory . And those files are common to both the directory copy source to destination with a numeric increment as extension. If in the destination there are more... (2 Replies)
Discussion started by: posix
2 Replies

6. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

7. Shell Programming and Scripting

Merging 2 files

Hi, I have got two files 1.txt 1111|apple| 2222|orange| 2.txt 1111|1234|000000000004356| 1111|1234|000000001111| 1111|1234|002000011112| 2222|5678|000000002222| 2222|9102|000000002222| I need to merge these two so that my out put looks like below: Search code being used should be... (4 Replies)
Discussion started by: jisha
4 Replies

8. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

9. UNIX for Dummies Questions & Answers

Merging files

Hi i have two files say file 1 contents are A B C D E I have file2 contents are B E F G C K I want to have new file like A B (4 Replies)
Discussion started by: ssuresh1999
4 Replies

10. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies
Login or Register to Ask a Question