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]

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

Another approach:

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.

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

Hi,

  1. SUBSEP:

From Effective AWK Programming:

It's the same as f1[FNR,i].

  1. The next statement:

From Effective AWK Programming:

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:

[...]
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:

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:

	
[...]
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.

Hi.

See Gawk: Effective AWK Programming - GNU Project - Free Software Foundation (FSF) for many formats of the book on-line ... cheers, drl

HI,

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

Thanx Alot i am really gratefull to you that you helps me alot.
Do ou know any web site from where i can download the trainning video of PERL and SHELL SCRIPTING.
Regards,
shary