multiple file data to one file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers multiple file data to one file
# 1  
Old 01-12-2009
multiple file data to one file

Hi guys
New to scripting, I have many data files and I'm having real trouble with a script for the following:

For all files that end in *.dat
i want to get the mean of column6 ($6/NR)

and send each mean to column2 in a new file where column1 would represent the name of the original files
e.g. 090112 4.32
090111 5.67
090110 3.70

thirdly I'd like to add a third column to this file which is:
$3 = 2 * exp(3 * $2 -1)

I'd really appreciate any help as I've been banging my head against the wall.

Larry
# 2  
Old 01-12-2009
One way:

Code:
awk '
NR==1{f=FILENAME;s=$6;next}
FILENAME != f {
  printf("%s %d %d\n", substr(f,1,match(f,".")), s/n, 2*exp(3*(s/n)-1))
  f=FILENAME
  s=0
} 
{s+=$6;n=FNR}
END{
  printf("%s %d %d\n", substr(f,1,match(f,".")), s/n, 2*exp(3*(s/n)-1))
}' *.dat

Regards
# 3  
Old 01-12-2009
Thanks very much Franklin52, running that script initially yields something like this (see below) so there must be a few things I'll need to change but it's certainly pointed me in the right direction.

0 0/n0 1 0/n0 1 0/n0 1 0/n0 0 0/n0 0 0/n0 1 0/n0 0 0/n0 1 0/n0 1 0/n0 0 0/n0 1 0/n0 1 0/n0 1 1/n0 0 0/n0 1 0/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 0/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 0/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 1/n0 1 2/n0 1 1/n0 1 1/n0 1 2/n0 1 2/n0 1 1/n0 1 2/n0 1 1/n0 1 2/n0 1 1/n0 1 1/n0 1 3/n0 1 1/

Regards Larry
# 4  
Old 01-12-2009
I assume the format of the .dat files are identical, right? Can you post a snippet of the output of one of the dat files?

Regards
# 5  
Old 01-12-2009
The format is identical though I just realised the data is separated by commas:

1018.62, 270.53, 4.76, 75.70, 8.72, 2.122, 0, 16:07:53, 2008/12/17, 0.006
1018.87, 271.95, 4.30, 75.82, 8.72, 1.984, 0, 16:12:53, 2008/12/17, 0.001
1018.87, 275.13, 4.47, 75.82, 9.08, 1.874, 0, 16:17:53, 2008/12/17, 0.002
1018.62, 281.05, 5.92, 76.19, 8.96, 1.752, 0, 16:22:53, 2008/12/17, 0.000
1018.87, 277.98, 4.62, 77.17, 8.72, 1.563, 0, 16:27:53, 2008/12/17, 0.002
# 6  
Old 01-12-2009
Change the fieldseparator:

Code:
awk -F, '
NR==1{f=FILENAME;s=$6;next}
FILENAME != f {
  printf("%s %d %d\n", substr(f,1,match(f,".")), s/n, 2*exp(3*(s/n)-1))
  f=FILENAME
  s=0
} 
{s+=$6;n=FNR}
END{
  printf("%s %d %d\n", substr(f,1,match(f,".")), s/n, 2*exp(3*(s/n)-1))
}' *.dat

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. UNIX for Beginners Questions & Answers

Using bash script : How to Import data from a dsv file into multiple tables in mysql

HI I have a dsv file that looks like: <<BOF>> record_number|id_number|first name|last name|msisdn|network|points|card number|gender 312|9101011234011|Test Junior|Smith|071 123 4321|MTN|73|1241551413214444|M 313|9012023213011|Bob|Smith|27743334321|Vodacom|3|1231233232323244|M... (4 Replies)
Discussion started by: tera
4 Replies

3. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

4. Shell Programming and Scripting

Write data into existing '.xlsx' file that has multiple sheets

Hello, I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 1000 rows) from Row 3 onwards on the 2nd sheet. I have my code which can insert values into existing sheet but I am unable to write/import the contents of a text... (1 Reply)
Discussion started by: nans
1 Replies

5. Shell Programming and Scripting

Write data into existing '.xlsx' file that has multiple sheets

Hello, I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 1000 rows) from Row 3 onwards on the 2nd sheet. I have my code which can insert values into existing sheet but I am unable to write/import the contents of a text... (0 Replies)
Discussion started by: nans
0 Replies

6. Shell Programming and Scripting

awk - Multiple files - 1 file with multi-line data

Greetings experts, Have 2 input files, of which 1 file has 1 record per line; in 2nd file, multiple lines constitute 1 record; Hence declared the RS=";" Now in the first file which ends with ";" at each line of the line; But \nis also being considered as part of the data due to which I am... (1 Reply)
Discussion started by: chill3chee
1 Replies

7. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

8. Shell Programming and Scripting

Read multiple files, parse data and append to a file

Hi..Can anyone suggest a simple way of achieving this. I have several files which ends with extension .vcf . I will give example with two files In the below files, we are interested in File 1: 38 107 C 3 T 6 C/T 38 241 C 4 T 5 C/T 38 247 T 4 C 5 T/C 38 259 T 3 C 6 T/C... (8 Replies)
Discussion started by: empyrean
8 Replies

9. Shell Programming and Scripting

Using AWK to separate data from a large XML file into multiple files

I have a 500 MB XML file from a FileMaker database export, it's formatted horribly (no line breaks at all). The node structure is basically <FMPXMLRESULT> <METADATA> <FIELD att="............." id="..."/> </METADATA> <RESULTSET FOUND="1763457"> <ROW att="....." etc="...."> ... (16 Replies)
Discussion started by: JRy
16 Replies

10. Shell Programming and Scripting

Extract multiple repeated data from a text file

Hi, I need to extract data from a text file in which data has a pattern. I need to extract all repeated pattern and then save it to different files. example: input is: ST*867*000352214 BPT*00*1000352214*090311 SE*1*1 ST*867*000352215 BPT*00*1000352214*090311 SE*1*2 ... (5 Replies)
Discussion started by: apjneeraj
5 Replies
Login or Register to Ask a Question