Interpolation of two values in two different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interpolation of two values in two different files
# 1  
Old 12-15-2015
Interpolation of two values in two different files

Dear All,

I have two files which contain numerical data and strings. I want to create a new file that only revise numerical data from two files using interpolation. I guess AWK works, but I am new on AWK.

FileA.txt
Code:
.
.
index_2("0.1, 1, 2, 4, 8, 16, 32");
values("0.0330208, 0.0345557, 0.0409915" \
 "0.0329737, 0.0344924, 0.0483888", \
 "0.0336141, 0.0351873, 0.0370214");
.
.

FileB.txt
Code:
.
.
index_2("0.1, 1, 2, 4, 8, 16, 32");
 values("0.0624608, 0.0654286, 0.0684585", \
 "0.0707829, 0.0737413, 0.0767505", \
 "0.0880787, 0.0909583, 0.0939307");
.
.

FileNew.txt
Code:
.
.
index_2("0.1, 1, 2, 4, 8, 16, 32");
 values("(ValueA+ValueB)/2, (ValueA+ValueB)/2, (ValueA+ValueB)/2", \
 "(ValueA+ValueB)/2, (ValueA+ValueB)/2, (ValueA+ValueB)/2", \
 "(ValueA+ValueB)/2, (ValueA+ValueB)/2, (ValueA+ValueB)/2");
.
.

There are two tasks in my work.
1) Find "index_2(" using if statement
2) Create a new file that only change values (e.g., NewValue = (ValueA+ValueB)/2)

Please provide any comment for me. Thank you in advance.

Best,

Jaeyoung

Last edited by Scrutinizer; 12-15-2015 at 02:38 PM.. Reason: CODE tags
# 2  
Old 12-15-2015
Is this a homework assignment? (Homework and coursework questions can only be posted in the Homework and Coursework forum under special homework rules listed here.)

The quoting in your sample data is very strange. Instead of:
Code:
index_2("0.1, 1, 2, 4, 8, 16, 32");
values("0.0330208, 0.0345557, 0.0409915" \
  Why is there no comma after the closing quote on the line above?
 "0.0329737, 0.0344924, 0.0483888", \
 "0.0336141, 0.0351873, 0.0370214");

we usually see quoting like:
Code:
index_2("0.1", "1", "2", "4", "8", "16", "32");
values("0.0330208", "0.0345557", "0.0409915", \
 "0.0329737", "0.0344924", "0.0483888", \
 "0.0336141", "0.0351873", "0.0370214");

or (especially when dealing with numeric values) no quotes:
Code:
index_2(0.1, 1, 2, 4, 8, 16, 32);
values(0.0330208, 0.0345557, 0.0409915, \
 0.0329737, 0.0344924, 0.0483888, \
 0.0336141, 0.0351873, 0.0370214);

# 3  
Old 12-15-2015
Thank you for your replay. This is not a homework.

Data is given. Therefore, comma and closing quotes are not main problem.

Best,

Jaeyoung

---------- Post updated at 03:20 PM ---------- Previous update was at 03:14 PM ----------

Dear All,

Let me clarify my questions. I have two files which contain numbers and texts. Texts in two files are identical. I want to create a new file that has average of numbers from two files.

FileA.txt (more than 10000 lines and more than 1000 text and numbers)

Code:
textA
textB(10,2,2)
textC(2)
textD
.
.

FileB.txt (Texts are identical to FileA.txt)

Code:
textA
textB(0,0,4)
textC(4)
textD
.
.

FileNew.txt (Have averages from FileA and FileB.txt)

Code:
textA
textB(5,1,3)
textC(3)
textD
.
.

One request is that I don't want to change any text. Only numbers are needed to be changed.

I think AWK or diff work this job.

Best,

Jaeyoung

Last edited by Don Cragun; 12-15-2015 at 05:26 PM.. Reason: Add CODE and ICODE tags
# 4  
Old 12-15-2015
Quote:
Originally Posted by jypark22
Thank you for your replay. This is not a homework.

Data is given. Therefore, comma and closing quotes are not main problem.

Best,

Jaeyoung
Well, they usually are. Everything within quotes usually is considered ONE data point.
Are index_2 and values to be considered correlated? Would be difficult, as index_2 has 7 items while values has 9 (when all double quotes are removed).
Your leading and trailing dots imply that there's more contents in either input file. Which of these should make it into the output file?

Last edited by RudiC; 12-16-2015 at 08:23 AM.. Reason: missing parenthesis
# 5  
Old 12-15-2015
Hi, jypark22

Your last try at clarifying your request has made it a bit more confusing, at least for me.

I think that we need an example with a bit more extended information that would allow us to find what kind of pattern the real file has. One block example is not enough.

Could you confirm the pattern of the block to be? Is it like this in FileA.txt?

Code:
index_1("0.1, 1, 2, 4, 8, 16, 32");
values("0.0330207, 0.0345558, 0.0409915", \
 "0.0329737, 0.0344924, 0.0483888", \
 "0.0336141, 0.0351873, 0.0370214");
index_2("0.1, 1, 2, 4, 8, 16, 32");
values("0.0330208, 0.0345557, 0.0409915", \
 "0.0329737, 0.0344924, 0.0483888", \
 "0.0336141, 0.0351873, 0.0370214");
index_3("0.1, 1, 2, 4, 8, 16, 32");
values("0.0330203, 0.0345554, 0.0409915", \
 "0.0329737, 0.0344924, 0.0483888", \
 "0.0336141, 0.0351873, 0.0370214");

Would it ever have anything else in between block patterns?
# 6  
Old 12-15-2015
Please use code tags as required by forum rules!

For your samples in post#3, this might work:
Code:
awk -F"(" '
FNR==NR         {if ($2) T[$1] = $2
                 next
                }
$1 in T         {n = split ($2, N, ",")
                 n = split (T[$1], M, ",")
                 $2 = "("
                 for (i=1; i<=n; i++) $2 = sprintf ("%s%.f,", $2, (N[i]+M[i])/2)
                 sub (/,$/, ")", $2)
                }
1
' OFS="" file[12]
textA
textB(5,1,3)
textC(3)
textD

# 7  
Old 12-15-2015
Thank you for your comment, Aia and RudiC.

"index_2" is followed by numbers. There are no "index_1" and "index_2" in data.

Also, there is no relationship between texts. All texts in two files are identical. Only numbers are changed on two files.

Best,

Jaeyoung
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Stub Values In One Group Of Files With Actual Values From Another Group Of Files

I have two directories of files (new-config-files and old-config-files): new-config-files/this-db/config.inc.php new-config-files/that-db/config.inc.php new-config-files/old-db/config.inc.php new-config-files/new-db/config.inc.php new-config-files/random-database/config.inc.php etc. ... (4 Replies)
Discussion started by: spacegoose
4 Replies

2. Shell Programming and Scripting

An interpolation between two files

Dear all, I always appreciate your help. I am an electrical engineer. I am using a tool for timing analysis of a circuit. I would like to interpolate results from two timing reports at different voltages (0.945V and 0.78V). If voltage is decreased, data arrival time is increased. For... (4 Replies)
Discussion started by: jypark22
4 Replies

3. UNIX for Dummies Questions & Answers

Interpolation if there is no exact match for value

Dear all, could you help me with following question. There are two datasets (below). I need to find match between BP values from data1 and data2, and add corresponding CM value from data2 into data1. if there is not exact match, the corresponding CM value should be calculated using interpolation.... (20 Replies)
Discussion started by: kush
20 Replies

4. Shell Programming and Scripting

Expand & Interpolation

Dear All, I have input like this, infile: 10 464310.16 20 464309.44 30 464309.02 40 464316.93 ... ... Desired output per step: out_step01: 10 464310.16 11 12 13 14 (9 Replies)
Discussion started by: attila
9 Replies

5. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

6. Shell Programming and Scripting

Hi ! whether it is possible to do interpolation in scripting...

Hi ! Experts... I just wanted to know whether it is possible in scripting...to do interpolation.... if so....have a look on my data file I need temperature and salinity value with a bin size of 0.5 m output looks somewhat like this dep temp sal 0.5 25 0.077 1 25 ... (12 Replies)
Discussion started by: nex_asp
12 Replies

7. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

8. Shell Programming and Scripting

Interpolation using awk

Hi all, Consider I have a text file containing: 1003 60 1005 80 1100 110 Based on that file I need to create another file which is containing value from 1001 till 1100 which is a linear interpolation between two point (for 1004; 1006;1007 until 1109) and extrapolation based on 2... (7 Replies)
Discussion started by: ardy_yana
7 Replies

9. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

10. UNIX for Dummies Questions & Answers

variable interpolation

I've become obsessed with trying to get this to work. As of yet, I am unable to figure it out. Unfortunately, I don't have Linux or UNIX available when I get home. Anyone have tips for me on how I can pass param1 to ID via use of COUNTER and loop? thx. LIMIT=6 param1="999999999" export... (0 Replies)
Discussion started by: egkumpe
0 Replies
Login or Register to Ask a Question