The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to merge these two files? fedora Shell Programming and Scripting 3 02-12-2008 06:45 PM
merge files koti_rama Shell Programming and Scripting 5 12-24-2007 10:59 PM
use of sed over cat to merge files miwinter UNIX for Advanced & Expert Users 2 11-28-2007 01:36 PM
How to merge files lestat_ecuador Shell Programming and Scripting 3 06-07-2007 07:45 AM
help in merge files u263066 Shell Programming and Scripting 5 07-24-2006 04:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-29-2008
Unice Unice is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
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 (permalink)  
Old 08-29-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
Do not post questions without trying to solve the problem yourself based on your understanding.
Please read Simple rules of the UNIX.COM forums: before posting, especially 5 and 6.
  #3 (permalink)  
Old 08-29-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,794
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 (permalink)  
Old 08-29-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
What about..

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

  #5 (permalink)  
Old 09-01-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
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 (permalink)  
Old 09-22-2008
akil akil is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 50
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 (permalink)  
Old 09-22-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
Quote:
Originally Posted by danmero View Post
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0