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 excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 04:13 AM
Adding columns of two files chandra321 Shell Programming and Scripting 6 04-06-2007 06:36 AM
Adding files of numerical data Boucho High Level Programming 17 02-07-2006 01:29 PM
Question on files - adding commas at the end jingi1234 UNIX for Dummies Questions & Answers 5 09-27-2005 01:06 PM
Adding 3 Lines to Multiple c and h files with perl Lazzar Shell Programming and Scripting 2 10-28-2003 10:30 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-24-2007
Registered User
 

Join Date: Jan 2007
Posts: 56
adding two files

Dear Experts,

I am facing some problem.
I have two files, every field is separated by comma "," separator.
And the value is in numeric
FILEA
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17

FILE B

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17

So the output to be shown like this
COLUMN 1+COLUMN1,COULMUN2+COLUMN 2,…….ETC.
I want to add two files and the output should be save in to the third file
FIANAL OUTPUT.

FINAL OUTPUT
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34

Regards,
SHARY
[/b]
Reply With Quote
Forum Sponsor
  #2  
Old 11-24-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
awk -F"," 'BEGIN{ while( getline < "file_one" ) { split($0, th, ","); for(j=1; j<=NF; j++) { arr[i++]=$j } } }{ for(j=1; j<=NF; j++ ) { printf "%d ", arr[x++]+$j } printf "\n"; }' file_two
Reply With Quote
  #3  
Old 11-24-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Another approach:

Code:
awk 'NR == FNR {
				for (i=1; i<=NF; i++)
					f1[FNR SUBSEP i] = $i
					next
			}
{
	for (j=1; j<=NF; j++)
			$j = $j + f1[FNR SUBSEP j]
}1' FS="," OFS="," fileA fileB
Use nawk or /usr/xpg4/bin/awk on Solaris.

Last edited by radoulov; 11-24-2007 at 11:35 AM. Reason: adjusted
Reply With Quote
  #4  
Old 11-25-2007
Registered User
 

Join Date: Jan 2007
Posts: 56
awk 'NR == FNR {
for (i=1; i<=NF; i++)
f1[FNR SUBSEP i] = $i
next
}
{
for (j=1; j<=NF; j++)
$j = $j + f1[FNR SUBSEP j]
}1' FS="," OFS="," fileA fileB

Thanks alot it really works very fine.but i have never used SUBSEP and next. if you dont mind can you please explain me exactly what its doing and how the scripts work.
i searched alot but unable to understan it.

Regards,
SHARY
Reply With Quote
  #5  
Old 11-25-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Hi,

1. SUBSEP:

From Effective AWK Programming:
Quote:
SUBSEP is a subscript separator. It has the default value of "\034" and is
used to separate the parts of the indices of a multidimensional array.
Quote:
The default value of SUBSEP is the string "\034", which contains a nonprinting character
that is unlikely to appear in an awk program or in most input data. The usefulness of
choosing an unlikely character comes from the fact that index values that contain a string
matching SUBSEP can lead to combined strings that are ambiguous. Suppose that SUBSEP
is "@"; then ‘foo["a@b", "c"]’ and ‘foo["a", "b@c"]’ are indistinguishable because both
are actually stored as ‘foo["a@b@c"]’.
It's the same as f1[FNR,i].

2. The next statement:

From Effective AWK Programming:

Quote:
The next statement forces awk to immediately stop processing the current record and go
on to the next record. This means that no further rules are executed for the current record,
and the rest of the current rule’s action isn’t executed.
You need it here in order to execute only the NR == FNR rule on the first file, so the next rule/action will go directly to the second file:

Code:
[...]
for (j=1; j<=NF; j++)
			$j = $j + f1[FNR SUBSEP j]
[...]

The scope is to read the first file and populate an array (f1) whose indices are the FNR
(current record number in the current file) and the number of the fields like:

Code:
record 1 field 1 == 1
record 1 field 2 == 2
...

record n field n == n
After that we read the second file and sum its fields
with those matching the elements of the f1 array:

Code:
	
[...]
for (j=1; j<=NF; j++)
			$j = $j + f1[FNR SUBSEP j]
[...]
Note that we're only recalculating the fields ($j = $j + f1[FNR SUBSEP j]),
so after that we're free to set OFS.

Last edited by radoulov; 11-25-2007 at 08:19 AM.
Reply With Quote
  #6  
Old 11-25-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.

See Gawk: Effective AWK Programming - GNU Project - Free Software Foundation (FSF) for many formats of the book on-line ... cheers, drl
Reply With Quote
  #7  
Old 11-25-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
awk

HI,

code:
Code:
nawk -v l=3 'BEGIN{
FS=","
}
{
if (NR<=l)
{
        for (i=1;i<=NF;i++)
                sum[NR,i]=$i
}
else
{
        for (i=1;i<=NF;i++)
        {
                sum[NR-l,i]=sum[NR-l,i]+$i
                printf("%s,",sum[NR-l,i])
        }
        print ""
}
}' file1 file2
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:05 AM.


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

Content Relevant URLs by vBSEO 3.2.0