Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have two CSV files and i have the requirement to calculate the sum of the 8th column from File1 and File2 for every row. The two CSV files have exactly 24 rows each and to simplify the things i have extracted the 8th row of each CSV file using:

awk -F, '{ print $8 }' >> FILE1
awk -F, '{ print $8 }' >> FILE2

Now, i have 2 file data in the following format:

File1

1
2
3
4

File2

2
3
4
5

How can i make the calculations in the way that

File1.Col1 + File2.Col1 = File3.Col1
File1.Col2 + File2.Col2 = File3.Col2
File1.Col3 + File2.Col3 = File3.Col3
.
.
.

resulting in:

File3

3
5
7
9

and so on using Bash Shell script because rest of my processing is being done in the same.

share|improve this question
    
Don't you mean File1.Row1 + File2.Row1 = File3.Row1, etc? –  PM 2Ring Aug 5 at 12:13
1  
I suggest doing all of your processing in an awk script; there's no need to extract the 8th columns of your CSV files into temporary files. To read two or more files in parallel you can use awk's getline statement. –  PM 2Ring Aug 5 at 12:17
    
Ankit: If the below answer has solved your issue please accept it & let the post be solved. It'll help others –  neuron Aug 6 at 18:24
    
Sure. Thanks for your answer Neuron. –  Ankit Vashistha Aug 7 at 5:30

3 Answers 3

up vote 3 down vote accepted

paste File1 File2 | awk '{ print $1 + $2; }' > File3

share|improve this answer
    
Why do we need this ; in print statement? –  Ankit Vashistha Aug 7 at 5:31
    
Not required for a one liner, I had put some sanity check after that, forgot to remove it. –  neuron Aug 7 at 6:43

for avoiding an intermediate file, use:

paste <( awk -F, '{ print $8 }' original_file1 ) <( awk -F, '{ print $8 }' original_file2 ) | awk '{print $1+$2}' > file3
share|improve this answer

if both field are in 8th column among 24

 paste originalfile1 originalfile2 | awk '{print $8+$32 ; }' > file3 
share|improve this answer
1  
It's 24 rows as per the OP :) –  neuron Aug 5 at 15:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.