Add and divide each numbers with the added number

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add and divide each numbers with the added number
# 1  
Old 10-04-2011
Add and divide each numbers with the added number

Hi All,

I am stuck with this problem.

I have some 100000 (.dat) 1.dat, 2.dat,3.dat etc until 100000.dat files which look like this:


All my files 1.dat until 100000.dat look the same but with different numbers.

I have to first add all the numbers in each file and divide each number with that number and store the result in .div file.

This is what I mean:
Lets take above example: 1.dat
1. I add all the numbers present in 1.dat. The result is: 15.53

2. Now I divide each number in this 1.dat file with 15.53

That is


and my final 1.div will look like this:

I have to follow the same procedure for all my other files that is create 2.div, 3.div for its corresponding .dat files that is for 4.dat, I create 4.div, 5.dat, I create 5.div and so on.

I have tried this but I guess there are minor mistakes. Moreover, it is not iterating over all the files in the directory.

BEGIN{ FS="," }
    for(i=1;i <=  NF;i++)
    for (i=1;i <= numberColumn;i++)

I am using Linux with BASH.
# 2  
Old 10-04-2011
Try this...

for file in *.dat
  out_file=$( echo ${file%%.*} ).div
  awk -v out_file=$out_file '
  { a=a+$0; b[++i]=$0} END{for(j=1;j<=i;j++){print b[j]/a > out_file} } ' $file

This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Divide the numbers in file

Dear ALL, I have sample file : tx_bytes: 2422, tx_packets: 13, uptime: 16119, tx_bytes: 2342, tx_packets: 14, uptime: 11009, tx_bytes: 252, tx_packets: 12, uptime: 3113, my formula : minutes=$(( uptime/60%60 )) hours=$(( uptime/60/60%24 )) (3 Replies)
Discussion started by: gnulyn
3 Replies

2. Shell Programming and Scripting

Script to divide/expand first digit to show some numbers

Hello to everyone, I have this complex problem and I don't how to do it. I'm not sure if awk could be a good choice to do it or could be easiest in bash or perl. A kind of introduction would be: - I have a digit, lets say 3. - I can expand/spread out the digit 3 to cover all possible... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

3. Shell Programming and Scripting

Divide number of lines by the size of the same file. And create relational table.

I basically need to do what the title says. I have my text file. I'm still pretty new at this. At the moment I know that: 1. wc -l file.txt To get the number of lines. 2. ls -lh file.txt To get the file size. But I need to divide both numbers. Then I need to save the output in a... (7 Replies)
Discussion started by: PainMaker101
7 Replies

4. Shell Programming and Scripting

Is it possible to Divide a negative number in bash script

I am using a small script to divide some numbers in a given file and display the output in another file. I am getting the following error basename: invalid option -- '5' Try `basename --help' for more information. (standard_in) 1: syntax error The script is : #!/bin/bash for i in `cat... (4 Replies)
Discussion started by: kmnr877
4 Replies

5. Shell Programming and Scripting

Print numbers between two number ranges

Hi, I have a list.txt file with number ranges and want to print/save new all.txt file with all the numbers and between the numbers. == list.txt == 65936 65938 65942 && 65943 65945 ... (7 Replies)
Discussion started by: AK47
7 Replies

6. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

7. Shell Programming and Scripting

Closest Number from a Range of Numbers

out of a range of numbers, how can i pick out the number that is the closest to any arbitrary/random number that a user supplies? say the range of numbers are between 1 - 90000. but that doesn't mean each number exist between 1 - 90000. the range of numbers could be for example: 1, 3, 4, 6,... (6 Replies)
Discussion started by: SkySmart
6 Replies

8. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

9. Shell Programming and Scripting

Divide numbers into intervals

divide input values into specified number (-100 or -200) according to the key (a1 or a2 ....) For ex: if we give -100 in the command line it would create 100 number intervals (1-100, 100-200, 200-300) untill it covers the value 300 in a1. Note: It should work the same even with huge numbers... (3 Replies)
Discussion started by: ruby_sgp
3 Replies

10. AIX

How to replace many numbers with one number in a file

How to replace many numbers with one number in a file. Many numbers like 444565,454678,443298,etc. i want to replace these with one number (300).Please halp me out. (2 Replies)
Discussion started by: vpandey
2 Replies
Login or Register to Ask a Question