The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
adding columns Kelam_Magnus Shell Programming and Scripting 12 02-10-2009 10:56 AM
Adding columns to a file figaro UNIX for Dummies Questions & Answers 5 07-21-2008 01:50 AM
comparing files - adding/subtracting/formating columns oabdalla Shell Programming and Scripting 7 06-13-2008 03:20 AM
Perl: adding columns in CSV file with information in each dolo21taf Shell Programming and Scripting 1 03-05-2008 02:52 AM
Adding columns to excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 07:13 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 04-05-2007
chandra321 chandra321 is offline
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
  #2 (permalink)  
Old 04-05-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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
  #3 (permalink)  
Old 04-05-2007
chandra321 chandra321 is offline
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 08:39 PM..
  #4 (permalink)  
Old 04-05-2007
chandra321 chandra321 is offline
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 08:39 PM..
  #5 (permalink)  
Old 04-05-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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
  #6 (permalink)  
Old 04-06-2007
amon amon is offline
Registered User
  
 

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

Join Date: Apr 2007
Posts: 4
Thanks a lot. It was really useful.
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:39 PM.


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