# adding two files

**URL:** <https://community.unix.com/t/adding-two-files/178079>\
**Category:** Shell Programming and Scripting\
**Created:** [November 24, 2007, 11:11am UTC](https://community.unix.com/t/adding-two-files/178079 "2007-11-24T11:11:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/5_5575768a8748004e209b776fc1b2916d.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [November 24, 2007, 11:11am UTC](https://community.unix.com/t/adding-two-files/178079/1 "2007-11-24T11:11:30Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [November 24, 2007, 12:17pm UTC](https://community.unix.com/t/adding-two-files/178079/2 "2007-11-24T12:17:26Z")

</div>

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [November 24, 2007, 1:07pm UTC](https://community.unix.com/t/adding-two-files/178079/3 "2007-11-24T13:07:56Z")

</div>

Another approach:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/5_5575768a8748004e209b776fc1b2916d.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [November 25, 2007, 7:45am UTC](https://community.unix.com/t/adding-two-files/178079/4 "2007-11-25T07:45:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [November 25, 2007, 10:03am UTC](https://community.unix.com/t/adding-two-files/178079/5 "2007-11-25T10:03:27Z")

</div>

Hi,

1. SUBSEP:

From Effective AWK Programming:

> [@](#):
>
> 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.

> [@](#):
>
> 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].

1. The next statement:

From Effective AWK Programming:

> [@](#):
>
> 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:

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

```plaintext
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:

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

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [November 25, 2007, 12:21pm UTC](https://community.unix.com/t/adding-two-files/178079/6 "2007-11-25T12:21:15Z")

</div>

Hi.

See [Gawk: Effective AWK Programming - GNU Project - Free Software Foundation (FSF)](http://www.gnu.org/manual/gawk/) for many formats of the book on-line ... cheers, drl

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/5_5575768a8748004e209b776fc1b2916d.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [November 25, 2007, 8:59pm UTC](https://community.unix.com/t/adding-two-files/178079/7 "2007-11-25T20:59:36Z")

</div>

HI,

code:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/5_5575768a8748004e209b776fc1b2916d.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [November 26, 2007, 1:23am UTC](https://community.unix.com/t/adding-two-files/178079/8 "2007-11-26T01:23:53Z")

</div>

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
