![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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. |
|
||||
|
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";
}
|
|
||||
|
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 |
|
||||
|
Quote:
Code:
awk '{a[$1 FS $2]+=$3}END{for(i in a) print i, a[i]}' file1 file2 file3 .... fileX> output_file
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|