adding two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding two files
# 1  
Old 11-24-2007
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]
# 2  
Old 11-24-2007
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

# 3  
Old 11-24-2007
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 02:35 PM.. Reason: adjusted
# 4  
Old 11-25-2007
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
# 5  
Old 11-25-2007
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 11:19 AM..
# 6  
Old 11-25-2007
Hi.

See Gawk: Effective AWK Programming - GNU Project - Free Software Foundation (FSF) for many formats of the book on-line ... cheers, drl
# 7  
Old 11-25-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding two files

I have a file which entry is below cat file1 user1 user2 user3 another file, which entry is below cat file2 Proj1 Fin Proj2 Marketing I want O/P as below Delete User Profile "user1" FROM Proj1 Fin Delete User Profile "user1" FROM Proj2 Marketing Delete User Profile "user2"... (4 Replies)
Discussion started by: anshu ranjan
4 Replies

2. UNIX for Dummies Questions & Answers

Adding unzipped files to a Zip

Hey all, I have a zip file which I received, and I need to replace one of the files inside of it. I tried the obvious solution of unzipping the zip, replacing the file, and rezipping, but the following happened: Original Zip Size: 79MB Unzipped Size 80MB New Zip: 36MB When I feed the... (2 Replies)
Discussion started by: DeanLeitersdorf
2 Replies

3. Red Hat

RPM package, conditional adding files in "%files" section

Hello, Is it possible to specify a list of files to be included in an RPM package (section "% files") according to some condtions. For example, if a particular condition is true, do not include the file "X". If not include it. Thank you for your help. Regards (1 Reply)
Discussion started by: louzorios
1 Replies

4. Shell Programming and Scripting

Help with joining files and adding headers to files

Hi, I have about 20 tab delimited text files that have non sequential numbering such as: UCD2.summary.txt UCD45.summary.txt UCD56.summery.txt The first column of each file has the same number of lines and content. The next 2 column have data points: i.e UCD2.summary.txt: a 8.9 ... (8 Replies)
Discussion started by: rrdavis
8 Replies

5. Shell Programming and Scripting

Adding Multiple Files via Columns

I have a number of files with multiple rows that I need to add together. Let say I have 10 files: Each file has a great number of rows and columns. I need to add these files together the following way. In other words, If, for example, file A occupies Columns 1 to 19, I want to add file B... (7 Replies)
Discussion started by: Ernst
7 Replies

6. UNIX for Dummies Questions & Answers

Adding new columns to txt files

Dear all, I have a question. I have a txt.file as below. i want to add 3 more columns: column3=conlum 2*column2; column4=(1-column2)*(1-column2); column5=1-column3-column4. Do you know how to do it? Thanks a lot! file: column1 column2 a 1 b 20 c 30 d 3 ... (2 Replies)
Discussion started by: forevertl
2 Replies

7. Shell Programming and Scripting

Adding the trailers in two files

I have two files File1 has trailer as below TR|2|120 File2 has trailer as below TR|1|100 it should add both the fields from trailer of file 1 and 2....so that file2 has a trailer as TR|3|220 (3 Replies)
Discussion started by: Deepak62828r
3 Replies

8. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

9. Shell Programming and Scripting

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 (7 Replies)
Discussion started by: chandra321
7 Replies

10. Shell Programming and Scripting

awk adding columns from different files

Hi, I have two files and I need to add column 3 of file1 to column 3 of file 2 > file3 I also need to repeat for column 4. Thanks (1 Reply)
Discussion started by: dsstamps
1 Replies
Login or Register to Ask a Question