awk or perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or perl
# 8  
Old 11-07-2008
Thank you very much for your great help.
assume we have a graph (time, y) axis:

y(i+1) t(i+1)
/
y /
*
/ t
/
y(i), t(i)


y is the new point on the line at time t.
rotuer1.dat
10 2
20 4
30 4
40 5
50 4
60 2
sender1.dat
15 2
30 5
45 4
60 3
Assume that the t is between t(i) =10 and t(i+1)=20, which is 15 from sender1.dat. t could have also the same value e.g., 30 and 60.
y = y(i) + {y(i+1) - y(i)} {t-t(i)}/ (t(i+1)-t(i))
y = 2 + {4-2} (15-10)/{20-10}

y = 3;

estimated value is at time t is:
e = 3 - 2 = 1
in this case, I did try what you told me, but it didn't work. I will try to fix now. I will let you know with good news soon.


again thank you very much...



Quote:
Originally Posted by otheus
I'm sorry, but "t" is still undefined. Just replace "xxx" below.

Code:
sort -n  router2.dat sender.dat |
awk '{ yi=$2; ti=$1; y=ylast + ( yi - ylast ) * ( xxx - tlast ) / ( ti - tlast ); print ti,y; ylast=yi; tlast=ti; }'

# 9  
Old 11-08-2008
So is "t" derived or is it read from a file or is it user-defined?

I tried t = (yi+ylast)/2 which seemed to produce reasonable results.
# 10  
Old 11-08-2008
thank you very much for your reply...

yes, it is derived from the files. for example

As you said t = {t(i+1) - t(i)} / 2 and the new y = y(i) + {(y(i+1) - y(i))(t-t(i))}/{t(i+1) - t(i)}, if file "sender.dat" t has (15,25,35,45,55)

y
|-------------5-------------5
|---------4-------4----4 -----------4----4
|---------------------------------------------
|-2---2-------------------------2------------2
|------------------------------------------------> t
-10--15--20--25--30--35--40--45--50--55--60

For example: y(i) = 2, y(i+1)=4, t(i)=10, and y(i+1)=20;
the new interpolate value are given?
y(1) = 2 + {(4-2)
(15-10)}/(20-10) = 3
y(2) = 4 + {(4-4)(25-20)/(30-20) = 4



how about when t != {t(i+1) - t(i)} / 2?
y
|
|=====4===4======4============4
|=3==========3=======3====3
|===============================2====2
|------------------------------------------------> t
-10---13---20---26--30---39--40-----50-52---60

Assume that we have the following time t (column 1 in each file) for the both nodes:
router.dat sender.dat
10 13
20 26
30 39
40 52
50
60


For example: y(i) = 2, y(i+1)=4, t(i)=10, and y(i+1)=20;
the new interpolate value are given:
y(1) = 3 + {(4-3)
(13-10)}/(20-10) = 3.3
y(2) = 4 + {(4-4)(26-20)/(30-20) = 4
y(3) = 4 + {(3-4)(39-30)/(40-30) = 3.1


I did try this one, but it gave me wrong answer. I will try to change:
{ yi=$2; ti=$1; t=(yi + ylast)/2; y=ylast + ( yi - ylast ) * ( t - tlast ) / ( ti - tlast ); print t,ti,y; ylast=yi; tlast=ti; }








Quote:
Originally Posted by otheus
So is "t" derived or is it read from a file or is it user-defined?

I tried t = (yi+ylast)/2 which seemed to produce reasonable results.
# 11  
Old 11-08-2008
There's a big difference between
Code:
t=(t(i+1) - t(i)) / 2

and
Code:
t=(t(i+1) + t(i)) / 2

In the first, t represents the mean difference between the two ti values, whereas in the second, t represents the mean of the two ti values.

Anyway, you're en route to figuring out this problem. Good luck!
# 12  
Old 11-11-2008
I am so sorry for that I sent the answer without check it. You right it is t=(t(i+1) + t(i)) / 2.
I would appreciate it if you could check this again.

y = y(i) + {(y(i+1) - y(i)) (t - t(i))} / {t(i+1) - t(i)}
I used the sort function, which produced the following results

90.0 0.00
120.0 22.95
180.0 21.11
240.0 21.94
270.0 40.00
300.0 19.95
360.0 30.00
420.0 21.11
450.0 45.45
480.0 22.51
540.0 30.00
600.0 58.11
630.0 45.45
660.0 46.48
720.0 40.00
720.0 45.58
780.0 49.12

where t is from the first file (router1.dat), as shown in the above output.
In this case, there are only one point between y(i+1) and Y(i). For example 90
120 180 140 270


awk '{ yi=$2; ti=$1; y=ylast + ( yi - ylast ) * ( t - tlast ) / ( ti - tlast ); print ti,y; ylast=yi; tlast=ti; }'
is it possible to use an array: t = ti[i+1] or any other options?

Again, thank you very much for your help.


Quote:
Originally Posted by otheus
There's a big difference between
Code:
t=(t(i+1) - t(i)) / 2

and
Code:
t=(t(i+1) + t(i)) / 2

In the first, t represents the mean difference between the two ti values, whereas in the second, t represents the mean of the two ti values.

Anyway, you're en route to figuring out this problem. Good luck!
# 13  
Old 11-12-2008
Ah so you need multiple interprolations between t(i) and t(i+1)? I really don't know how to do that. OR rather, I can imagine several possibilities, depending on what you need.

Anyway, the results you posted look wrong. I suspect that "t" is still not being calculated correctly. An additional problem is that you have an oscillating input, not a non-decreasing one. Thus, you cannot rely on tlast, because your input might be at a local minimum. Interporlation as I understand it is only good for non-increasing or non-decreasing curves.
# 14  
Old 11-12-2008
thank you very much for your reply. I did try to make it easy. so that it wouldn't make any problems.
router data
60.0 21.62
120.0 22.95
180.0 21.11
240.0 21.94
300.0 19.95
360.0 19.96
420.0 21.11
480.0 22.51
540.0 59.96
sender data
90.0 0.00
180.0 9.09
270.0 40.00
360.0 30.00
450.0 45.45
540.0 30.00

I had to change the simulation timer for the router and sender side. According to the results, there is only one interpolate point between y(i) and y(i+t). In this case, t is interpolate time, which is from router.dat file. could I pass the t value without sorting the two files? any suggestion is helpful. again thank you for your help.




Quote:
Originally Posted by otheus
Ah so you need multiple interprolations between t(i) and t(i+1)? I really don't know how to do that. OR rather, I can imagine several possibilities, depending on what you need.

Anyway, the results you posted look wrong. I suspect that "t" is still not being calculated correctly. An additional problem is that you have an oscillating input, not a non-decreasing one. Thus, you cannot rely on tlast, because your input might be at a local minimum. Interporlation as I understand it is only good for non-increasing or non-decreasing curves.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting awk to perl

Hello. I'm trying to convert an awk script I wrote to perl (which I just started self-teaching). I tried the a2p command but I couldn't make sense of most of it. Here was the awk code: BEGIN{ FS = "," print "NAME\tLOW\tHIGH\tAVERAGE" a=0 } { if(a==0){ a+=1 (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

3. Shell Programming and Scripting

awk and perl grouping.

Hello folks. After awk, i have decided to start to learn perl, and i need some help. I have following output : 1 a 1 b 2 k 2 f 3 s 3 p Now with awk i get desired output by issuing : awk ' { a = a FS $2 } END { for ( i in a) print i,a }' input 1 a b 2 k f 3 s p Can... (1 Reply)
Discussion started by: Peasant
1 Replies

4. Shell Programming and Scripting

get value between <abc and > by perl, awk

Hi Everyone, cat 1.txt a <abc b vfff c 000> d 4444 the output is: <abcvfff000> by using perl or awk, can get the value betwee "<abc" and ">", assume 1.txt has lots of those tags, so the output can filter out all those values. Please advice. Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

5. Shell Programming and Scripting

awk - to -- PERL

DEAR ALL, i am using following command to fetch some records from more then 50000 files... command is taking more time .... can i have perl command for the same...( i am New to perl ) awk -F "|" '{ if($4==3244898 && $5==5000185) print $66}' *.DATA (2 Replies)
Discussion started by: arvindng
2 Replies

6. Shell Programming and Scripting

awk vs perl

awk -F "|" '{print $2$3$4 upto $30}' file1 > file2 Same logic, i want to write it in perl I tried #!/bin/usr/perl my $line; open FH, "<file1" or die " Can't open file $!"; open FH1, ">file2" or die "Can't open file "; while (<FH1>){ $line = (split /\|/,$_); print FH2 $line;... (3 Replies)
Discussion started by: pritish.sas
3 Replies

7. Shell Programming and Scripting

Using perl grep and awk

I have a script to get server information i wrote in perl because i would like to learn it (and I use it for work). It works great, however i would like to know if there is a good way to reduce the following line. Sean (6 Replies)
Discussion started by: insania
6 Replies

8. Shell Programming and Scripting

awk command with PERL

Hi All, My Input file looks like below: Input: 100,200,300 $fw=`head -1 test.csv | awk -F, '{print \$1}'`; $fw="'$fw" $fw="$fw'" print $fw Output:'100' I want the first field to be printed in single quotes ('') like above. I could get the ouptput but the problem is single... (6 Replies)
Discussion started by: kmkbuddy_1983
6 Replies

9. Shell Programming and Scripting

awk to perl convert

Dear Collegue Do anybody help me to convert this AWK script to Perl script { for (i = 2; i <= length ($0); i++) { x = substr($0, i , 1) if (c > 0) { b = b x if (x == "(") c++ ... (2 Replies)
Discussion started by: jaganadh
2 Replies

10. Shell Programming and Scripting

AWK embeded PERL

Hello Guys, I am refering to this great forum once again for help. After various attempts at this, I am still failing to obtain the desire effect. I have to write a perl script which: 1- reads two values from the user (e.g name & passwd) 2- check each value against a file containing the... (7 Replies)
Discussion started by: bionicfysh
7 Replies
Login or Register to Ask a Question