Adding files of numerical data


 
Thread Tools Search this Thread
Top Forums Programming Adding files of numerical data
# 1  
Old 02-02-2006
Adding files of numerical data

Hi I was hoping that maybe someone could help me with a small piece of C code. I have a number of files, which are all of similar layout ie. three lines of text and 5-6 columns of numerical data. I need to add each of the elements of the second column in one file to their counterparts in the second column of the other file. I must add them in the ratio of 3:1 so I also need to multiply one column by a scalar. So I need to extract the files (into an array?), manipulate the data, add the array elements into a new array, and return this as a file. I think. Any help anyone could provide would be greatly appreciated as this problem is really holding me up. Thanks very much.
# 2  
Old 02-02-2006
is it is constraint that it has to be done in c? can you opt for shell script? (You have this posted in the C forums)
well not that it cant be done in c. but parsing the files is gonna take away all the zeal.
if you can consider scripting you can think of awk (nawk) as substitutes, let us know
# 3  
Old 02-03-2006
C not necesary

No i suppose C isn't necessarily the only option. The reason i was opting for it was its the only language i have any knowledge of; minimal as that knowledge is. Programming is something i've had little contact with and i've only a pretty basic understanding of it. So whatever you reckon would be the easiest method to solving my problem. And this is probably a very "rookie" question, but whats awk/nawk? Thanks for your reply.
# 4  
Old 02-03-2006
Post some sample data. Also tell us what kind of unix you have. The command uname -a will tell you.
# 5  
Old 02-03-2006
Its Red Hat, version 9 i think. I don't have the actual data with me now but i can give you an example:

Code:
% This is a file containing the flux at varying wavelengths
% of a young massive star which is surrounded by a dust cloud. 
% Wavelength       Flux        Temp       O.D.
      0              0           x         x
      5              3E-6        x         x
     10             16E-5        x         x
     15             87E-3        x         x
      :              :           :         :
    200              0           x         x

The elements in the first column are always the same. I need to add together the Flux values at each wavelength, in the ratio of say 3:2 and put the result into a new file. The other columns are irrelevent. Thanks a million for any help you can give me.

Last edited by Perderabo; 02-03-2006 at 03:52 PM.. Reason: Add code tags and realign columns for readability
# 6  
Old 02-03-2006
That looks all wrong. Not the way i typed it up at all. Sorry. There are 4 seperate values in each row, one under each of the headings. So in line 2 5 is under "wavelength" , 3E-6 is under "flux", and an x (insignificant value) under both Temp and O.D.
# 7  
Old 02-03-2006
Code:
nawk '{ arr[$1] += $2 }
END {
  for ( i in arr )
     print i, arr[i]
}' myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Divide a numerical data column by a variable

Hello, I have two files, f1 and f2. f1 has 5 columns like so: a b c d 154 e f g h 365 ..... f2 has two columns, the first column contains the name of the above file and second column contains a constant which is to be used for division. e.g. file1 56 I want to divide the 5th... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

2. Shell Programming and Scripting

how to extract data from numbered files using linux in the numerical order-

Hi experts, I have a list of files containing forces as the only number as follows. Force1.txt Force2.txt Force3.txt Force4.txt Force5.txt . . . . . . . . . Force100.txt I want to put all the data(only a number ) in these forces files in the file with the same order like 1,2,3 ..100 .... (2 Replies)
Discussion started by: hamnsan
2 Replies

3. Shell Programming and Scripting

Deleting particular files with a numerical suffix

Hello I have a directory with a list of files which have a particular numerical suffix. E.g filename_0 filename_1 filename_18500 filename_10000 I want to delete all files from this directory which have a filename which have a numerical suffix greater than 10540. So any files... (5 Replies)
Discussion started by: kamal_p_99
5 Replies

4. UNIX for Dummies Questions & Answers

List files according to the numerical value

Hi, I have a large number of files which are named as follows. VF_50, VF_100, VF_150, VF_250, VF_300, VF_350, VF_400, VF_450, VF_500. When I do an 'ls' it arranges the files in the following way VF_100, VF_150, VF_250, VF_300, VF_350, VF_400, VF_450, VF_50, VF_500. Is there a way to... (2 Replies)
Discussion started by: lost.identity
2 Replies

5. UNIX for Dummies Questions & Answers

moving/copying files in a numerical order

Hi I am newbie to unix scripting, but i have enough knowledge to understand. I have a specific questions like, I use to collect like 3500 files per experiment, each one named like data_001.img.. data_002.img data_003.img .... data_3500.img I would like to move every 12 files in the 3500... (3 Replies)
Discussion started by: wpat
3 Replies

6. Shell Programming and Scripting

script to sort a string of numerical data in set fields

So, I will be working with someone and basically we are trying to build a form that is submitted most likely via the web and the data is just a string of numbers. like: 19383882872201110929282821818182827349190102837364718191001932873711 Now, each number is part of a numerical value of... (4 Replies)
Discussion started by: tlarkin
4 Replies

7. Shell Programming and Scripting

compare files by numerical value

Hi everyone, I would love to have a script that does the following: I have one file that looks like that: ATOM 1 BB SER 1 1 -31.958 -25.125 -11.061 1.00 0.00 ATOM 3 BB GLY 1 2 -32.079 -26.085 -14.466 1.00 0.00 ATOM 4 BB VAL 1 3 ... (1 Reply)
Discussion started by: s-layer
1 Replies

8. Shell Programming and Scripting

Reading Numerical Binary Data using KSH

Hi, I've searched and couldn't find anyone else with this problem. Is there anyway (preferably using ksh - but other script languages would do) that I can read in binary float data into a text file. The data (arrays from various stages of radar processing) comes in various formats, but mainly... (3 Replies)
Discussion started by: Jonny2Vests
3 Replies

9. Shell Programming and Scripting

How to strip non numerical data out of file?

Hi, How can I remove all non numerical data from line, so I don't want to delete the line but to have only the numbers. e.g.: ######### 123 aaa124 125bbb 126 127 ######### So I want all the leading and trailing non numerical stuff(letters/white space/tabs anything else except... (10 Replies)
Discussion started by: Juha
10 Replies

10. Shell Programming and Scripting

Listing files in numerical order

Hi, I'm trying to write a ksh script to copy a specified number of files from one directory to another. The files are named in the convention <switchname>_log.<num> and the numbers are sequential single digit onwards. I figured I could find some parameter for ls which would list the files in... (3 Replies)
Discussion started by: Steve_H
3 Replies
Login or Register to Ask a Question