How to merge and add?? Two Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge and add?? Two Files
# 1  
Old 08-29-2008
How to merge and add?? Two Files

Sorry noob here......

I have 2 files like this:

File A
8080000001 400
8080000002 300
8080000003 200


File B
8080000001 600
8080000002 200
8080000004 200


I want one file where

File OUT
8080000001 1000
8080000002 500
8080000003 200
8080000004 200

I appreciate any help with this.

Thanks
# 2  
Old 08-29-2008
Do not post questions without trying to solve the problem yourself based on your understanding.
Please read https://www.unix.com/unix-dummies-que...om-forums.html before posting, especially 5 and 6.
# 3  
Old 08-29-2008
start with this:
Code:
awk ' FILENAME=="fileb" {arr[$1]+=$2}
        FILENAME=="filea" {arr{$1]+=$2}
        END{for (i in arr {print i, arr[i] } ' filea fileb > filec

AND consider searching the forums
# 4  
Old 08-29-2008
What about..
Code:
awk '{a[$1]+=$2}END{for(i in a) print i, a[i]}' file1 file2 file3 .... fileX > output_file

# 5  
Old 09-01-2008
awk:

Code:
nawk '{
if(NR==FNR)
	arr[$1]=$2
else
	arr[$1]+=$2
}
END{
for(i in arr)
	print i" "arr[i]
}' a b

perl:

Code:
open(FH,"<a");
while(<FH>){
	$_=~tr/\n//d;
	@arr=split(" ",$_);
	$hash{$arr[0]}=$arr[1];
}
close(FH);
open(FH1,"<b");
while(<FH1>){
	$_=~tr/\n//d;
	@arr=split(" ",$_);
	$hash{$arr[0]}+=$arr[1];
}
close(FH1);
for $key (sort keys %hash){
	print $key," ",$hash{$key},"\n";
}

# 6  
Old 09-22-2008
Hi ,
I have 2 files and want to group by 1st and 2nd column using perl

Code:
File A
SGD 8080000001 400
USD 8080000002 300
HK 8080000003 200


File B
SGD 8080000001 600
SGD 8080000002 200
HK 8080000004 200


I want one file where

File OUT
SGD 8080000001 1000
USD 8080000002 300
SGD 8080000002 200
HK 8080000003 200
HK 8080000004 200

Thanks,
Akil
# 7  
Old 09-22-2008
Quote:
Originally Posted by danmero
What about..
Code:
awk '{a[$1]+=$2}END{for(i in a) print i, a[i]}' file1 file2 file3 .... fileX > output_file

Code:
awk '{a[$1 FS $2]+=$3}END{for(i in a) print i, a[i]}'  file1 file2 file3 .... fileX> output_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge 3 files

help with a command please i have 3 files that i need to merge in to a 4th file using awk or sed the files have more than 3 lines in them file 1 AAA BBB CCCfile 2 AAA BBB CCCfile 3 AAA BBB CCCi want the 4th file to look like this after merging AAA AAA AAA (3 Replies)
Discussion started by: bob123
3 Replies

2. Shell Programming and Scripting

Merge two files and add the count

I have two files with same layout which i want to merge, removing the duplicate header and adding the count in footer HD|09052014|09052014 data1|data2|data3 data1|data2|data3 data1|data2|data3 data1|data2|data3 TR|09052014|09052014|4 HD|09052014|09052014 dt1|dt2|dt3... (4 Replies)
Discussion started by: depakjan
4 Replies

3. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

4. Shell Programming and Scripting

How to merge two files?

Hi Gurus, I have two files as below file1 abc cde cdd cdf file2 123 234 345 456 I want to get abc 123 cde 234 cdd 345 (3 Replies)
Discussion started by: ken6503
3 Replies

5. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

6. Shell Programming and Scripting

merge files

hi, i've two files whcih i need to merge in below format. File1- DEFINE lc_driver CHAR(3) INITIALIZE gr_XXX_MAIN_TABLE_XXX.* TO NULL { EXECUTE p_XXX_MAIN_TABLE_XXX_ins USING gr_XXX_MAIN_TABLE_XXX.* } File2 - # field1 LET table1.field1= (8 Replies)
Discussion started by: dvah
8 Replies

7. UNIX for Dummies Questions & Answers

Merge files and add file name to the end of each line

Hello everybody, I'm trying to merge a lot of files, but I want to include the filename to the end of each line. I've tried to use cat, but I got stuck. My files are for example: file01.001 123456 aaa ddd ee 458741 eee fff ee file02.003 478596 uuu ddd ee 145269 ttt fff ee ... (4 Replies)
Discussion started by: ernesto561
4 Replies

8. Shell Programming and Scripting

How can i merge these two files into several...

Given are File A and File B File A has for example 5 lines: AAA BBB CCC DDD EEE File B has 3 lines: 111 222 333 How can i merge A and B into: 111 222 333 AAA (first line from A) then a new file: (4 Replies)
Discussion started by: Y-T
4 Replies

9. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

10. Shell Programming and Scripting

MERGE 13 files and add the file name at the end of each record

Hi Gurus, I have 13 comma(,) seperated files that i have to merge and create a single file which has file name attached at th end of each record in the out put file. Can any one please help me with writing a unix script with this issue? test1.dat BIG ID,Local ID,Bond... (2 Replies)
Discussion started by: vkr
2 Replies
Login or Register to Ask a Question