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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding columns to a file figaro UNIX for Dummies Questions & Answers 5 4 Weeks Ago 10:50 PM
comparing files - adding/subtracting/formating columns oabdalla Shell Programming and Scripting 7 06-13-2008 12:20 AM
Perl: adding columns in CSV file with information in each dolo21taf Shell Programming and Scripting 1 03-04-2008 10:52 PM
Adding columns to excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 03:13 AM
adding columns Kelam_Magnus Shell Programming and Scripting 9 01-25-2002 06:35 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-05-2007
Registered User
 

Join Date: Apr 2007
Posts: 4
Adding columns of two files

Hello everyone,

I have two files containing 6 columns and thousands of rows. I want to add them (i.e. first column of first file + first column of second file and so on) and print the output in a third file. Can you please help me.

Thanks a lot
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-05-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
assuming your fields are 'space/tab' separated AND you want to produce one cell adding all rows in all the files for a gven column.
Code:
nawk '
   {
      for(i=1; i<= NF; i++)
         arr[i] += $i
      nf=NF
   }
   END {
      for(i=1; i<=nf; i++)
         printf("%s%s", arr[i], (i<nf) ? OFS : "\n") 
   }
' file1 file2
Reply With Quote
  #3 (permalink)  
Old 04-05-2007
Registered User
 

Join Date: Apr 2007
Posts: 4
Thanks a lot for the help. But I am not sure I made myself clear. Suppose I have files A and B (shown below) with 2 rows and 3 columns -

I just want another file with the same dimensions, just adding the corresponding element in each file.

A
1 1 1
2 2 2

B
1 1 1
3 3 3

Output file C
2 2 2
5 5 5


Does this program do that?

Thanks a lot

Last edited by chandra321; 04-05-2007 at 05:39 PM.
Reply With Quote
  #4 (permalink)  
Old 04-05-2007
Registered User
 

Join Date: Apr 2007
Posts: 4
I am sorry. I was not clear in my first post.

Last edited by chandra321; 04-05-2007 at 05:39 PM.
Reply With Quote
  #5 (permalink)  
Old 04-05-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
Code:
nawk ' 
FNR==NR {
  for(i=1; i<=NF; i++)
    f1[FNR,i]=$i
  next
}
{
  for(i=1; i<=NF; i++)
    printf("%d%s", $i+f1[FNR,i], (i==NF) ? "\n" : FS);
}' fileA fileB
Reply With Quote
  #6 (permalink)  
Old 04-06-2007
Registered User
 

Join Date: Jan 2006
Posts: 71
cool work!!and usefull
Reply With Quote
  #7 (permalink)  
Old 04-06-2007
Registered User
 

Join Date: Apr 2007
Posts: 4
Thanks a lot. It was really useful.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0