Arithmetic: how to??


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Arithmetic: how to??
# 1  
Old 12-01-2009
Arithmetic: how to??

Hello all,

I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns.

What I want to do is to to sum the values in each row & column in every file.

The following explains what I want to do;

Input:
File1 File2 File3 ...
123 321 231

Output:
File4
675

Thanks.
# 2  
Old 12-01-2009
Assuming an input like this one:

Code:
% printf '%s\t%s\t%s\n' {1..9} >file1 >file2 
% head file[12]
==> file1 <==
1       2       3
4       5       6
7       8       9

==> file2 <==
1       2       3
4       5       6
7       8       9

You can use the following Perl script:

Code:
% perl -F'\t' -lane'
  $t += $_ for @F;
  END { print $t }
  ' file[12]
90

Or this shell pipeline:

Code:
cat file[12]|tr '\t' '\n'|paste -sd+|bc

# 3  
Old 12-01-2009
Thanks radoulov,

Mind explaining what the following syntax means?

Code:
cat file[12]|tr '\t' '\n'|paste -sd+|bc


Last edited by radoulov; 12-01-2009 at 11:51 AM.. Reason: Added code tags.
# 4  
Old 12-01-2009
Sure.

Code:
cat file[12] |   # concatenate the content of file1 and file2
  tr '\t' '\n' | # translate every tab into a newline
    paste -sd+ | # paste the incomming input serialy (-s) as it was one file at a time, delimiter is + (-d)
      bc         # pass the data to bc

# 5  
Old 12-01-2009
I tried the shell method. Does not work because when I compared the files, I do not get the result I expected.

For example, given a particular grid cell,

File1 (input)
131.63

File2 (input)
143.3

File3 (output)
131.63

The grid cell in File3 should have a value of 274.93 (if summed) or 137.465 (if averaged).
# 6  
Old 12-01-2009
You want to sum per file or you want the total from all files?
Could please post a sample from your input files (1-2 records, the exact fields) and the expected output?
# 7  
Old 12-01-2009
For a start, I'd like to total from all files. But I'd also like to perform other calculations like averaging etc.

Each file contains a 2000x2000 matrix. There are 1000 of such files. I'm giving a brief illustration of what I want to do and the desired output.

Code:
File1                 File2            Output
1 1 1                2 2 2            3 3 3
1 1 1      +        2 2 2      =    3 3 3
0 0 0                1 1 1            1 1 1

Of course, I want to perform the function for all 1000 files.

I hope this illustration helps. There is a similar thread in this forum but I can't seem to remember it.

...

---------- Post updated at 06:23 PM ---------- Previous update was at 06:21 PM ----------

The layout messed up my formatting. Here it is again.

File1 + File2 = output

File1
Code:
1 1 1
1 1 1
0 0 0

File2
Code:
2 2 2
2 2 2
1 1 1

Output
Code:
3 3 3
3 3 3
1 1 1



---------- Post updated at 06:55 PM ---------- Previous update was at 06:23 PM ----------

Found the link to simillar thread;
https://www.unix.com/shell-programmin...two-files.html

Which part of the code should I modify in order to
1. include all 1000 files?
2. perform other functions e.g. multiplication, subtraction, division

Thanks

Last edited by radoulov; 12-01-2009 at 02:24 PM.. Reason: Please use code tags!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

3. Shell Programming and Scripting

csh arithmetic ?

Hello, Could someone explain how this one is possible: # @ x = 10 - 11 + 3 # echo $x -4 I know that writing script using csh is bad idea, but I need to write few lines. thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

4. UNIX for Dummies Questions & Answers

String Arithmetic ?

Hello Experts, In my shell I need to perform some simple subtraction on a value returned as a result of the "wc" command. The code: scanFromLine="100" ## This is returned as string as a result of some operation totalLines=`wc -l "${latestLogFile}" | awk '{print $1}'` ## eg: 200 ... (2 Replies)
Discussion started by: hkansal
2 Replies

5. Shell Programming and Scripting

Arithmetic on timestamps

Hi Friends, please advise on shell script to add two time stamps for example : a=12:32 b=12:00 c=a+b=00:32 please help me to find shell script to add to two time stamps, as i need to convert time from EST to GMT or SST to prepare status of jobs in unix and to specify estimated time to... (3 Replies)
Discussion started by: balireddy_77
3 Replies

6. UNIX for Dummies Questions & Answers

Arithmetic Operators

Hello, I have a list of 'inputs' and i want to convert those on the second list named 'Desired Outputs', but i don't know how to do it? Inputs Desired Outputs 1 2 94 4 276 8 369 10 464 12 ... (0 Replies)
Discussion started by: filda
0 Replies

7. Shell Programming and Scripting

arithmetic in tcsh

Yes I know tcsh sucks for scripting and arithmetic but I have to write a script for multiple users and they all use tcsh. I have this variable that I 'set' with but pulling numbers off of stings with set STUFF = `grep string file | awk command` Now I would like to add up the numbers that... (4 Replies)
Discussion started by: gobi
4 Replies

8. Shell Programming and Scripting

Help with arithmetic operation

I am using egrep to extract numbers from a file and storing them as variables in a script. But I am not able to do any arithmetic operations on the variables using "expr" because it stores them as char and not integers. Here is my code and the error I get. Any help will be appreciated. #!/bin/sh... (3 Replies)
Discussion started by: emjayshaikh
3 Replies

9. Shell Programming and Scripting

Can I use wc -l with arithmetic expression?

Folks, I am wondering that i can use something like this in one line. For example, $((cat filename > wc -l) / 2) It doesn't work; how to get it work using command substitution? Moreover, is there any option for wc -l not to return filename after the line counts? wc -l filename would... (3 Replies)
Discussion started by: lalelle
3 Replies

10. UNIX for Advanced & Expert Users

time arithmetic

Can anyone help please. I am writing a kourne shell script and I am unsure how to do the following: I have extracted a time string from a logfile, and I have another time string I want to compare it to to see if it's later than the time I'm comparing with. i.e. expectedSLA="23:00:00", ... (2 Replies)
Discussion started by: csong2
2 Replies
Login or Register to Ask a Question