Column means for multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column means for multiple files
# 1  
Old 01-08-2009
Question Column means for multiple files

Hi all,

I have multiple dat files one for each day of the year and each with two lengthy columns. For each file, I wan't to perform a mathematical operation on the data of column 2 (red), then get the mean of these values and create a new two-column file where each row shows the mean value and the filename of the original file.

I came up with this but it won't work properly:

cat *dat | awk ‘{print sum+(2 * exp(4 * $2- 3))/NR ,FILENAME}' > newfilename

Any assistance would be much appreciated - I'm new to scripting

Many thanks

Larry
# 2  
Old 01-08-2009
NR is the number of records - across all input. FNR is for all files.

Do you want a mean per file or for all data? Show us a couple of lines of input and the desired output.
# 3  
Old 01-08-2009
Sorry, I didn't make that very clear:

The files (e.g. 090108.dat) are like so:

2.34, 0.24
3.45, 0.45
4.81, 0.63
...etc

I want to do (0.24+0.45+0.63)/3 which is 0.44, then get x = 2*exp(4*0.44-3) the and place this in a new file:

x1, 090108
x2, 090107
x3, 090106

So NR represents the number of rows in each file.

I think my problem lies in the formatting of the sum+ function and the brackets

thanks

Larry
# 4  
Old 01-08-2009
something along these lines.
nawk -f larry.awk file1 file2 fileN

larry.awk:
Code:
FNR==1 && NR!=1{
  arr[file] = 2*exp(4*(sum/fnr)-3)
  sum=fnr=0
}

FNR==1 { file=FILENAME}
{
   sum+=$2
   fnr=FNR
}
END {
  arr[file] = 2*exp(4*(sum/fnr)-3)

  for (i in arr)
    print arr[i], i
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Help with column delete from multiple files

sample .csv files with 7 columns.I want to delete the last column from each of the below files but retain their file names (1_ContractDocuments.csv,2_ContractDocuments.csv etc.) There can be more files like 3_ContractDocuments.csv , 4_ContractDocuments.csv . Can you please help source .csv... (5 Replies)
Discussion started by: paul1234
5 Replies

3. UNIX for Dummies Questions & Answers

Stack data from multiple files into one, with variable column files

Hello Gurus, Im new to scripting. Got struck with a file merge issue in Unix. Was looking for some direction and stumbled upon this site. I saw many great posts and replies but couldnt find a solution to my issue. Greatly appreciate any help.. I have three csv files -> Apex_10_Latest.csv,... (1 Reply)
Discussion started by: wamshi
1 Replies

4. Shell Programming and Scripting

Select multiple column from multiple files

Hi Friends, $ cat test1.txt emeka:1438 shelley:1439 dmeyer:1440 kurtarn:1441 abdul:1442 $ cat test2.txt 1:a 2:b 3:c 4:d $ cat test3.txt cat:dog:bat man:hot:cold (5 Replies)
Discussion started by: Jewel
5 Replies

5. Shell Programming and Scripting

Sum of a column in multiple files

I am performing the following operation on a file that looks like this 1000 0 10 479.0 1115478.07497 0.0 0.0 0.0872665 1000 10 20 1500.0 3470012.29304 0.0 0.0 0.261799 1000 20 30 2442.0 5676346.87758 0.0 0.0 0.436332 1000 30 40 3378.0 7737905.30957 0.0 0.0 0.610865 1000 40 50 4131.0... (2 Replies)
Discussion started by: kayak
2 Replies

6. Shell Programming and Scripting

Column extraction from multiple files to multiple files

I have roughly ~30 .txt files in a directory which all have unique names. These files all contain text arranged in columns separated by whitespace (example file: [#YY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS TIDE #yr mo dy hr mn degT m/s m/s m sec ... (5 Replies)
Discussion started by: aozgaa
5 Replies

7. Shell Programming and Scripting

Average of a column in multiple files

I have several sequential files with name stat.1000, stat.1001....to stat.1020 with a format like this 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0... (6 Replies)
Discussion started by: kayak
6 Replies

8. UNIX for Advanced & Expert Users

merge two column multiple files into one

Hi I have multiple files each with two columns and I need to combine all those file into a tab delimited file. (multiple entry with same name separated by a comma) The content of the files are as follows: --- file1.txt: name var1 aaa xx aaa gg bbb yy ddd zz --- file2.txt ... (8 Replies)
Discussion started by: mary271
8 Replies

9. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies
Login or Register to Ask a Question