awk or perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or perl
# 1  
Old 10-26-2008
Error awk or perl

hi all
I am new to awk/perl I would appreciate it if you could help!
how do I read a sub folder and their files.
e.g. simulation/10ms/router1.dat, router2.dat,..., router16.dat
simulation/100ms/router1.dat, router2.dat,..., router16.dat
simulation/300ms/router1.dat, router2.dat,..., router16.dat

1. I need to read and plot the number columns form each file and sub folder:
simulation/10ms/router2.dat, ..., simulation/1500ms/router2.dat

2. how do I calculate and plot the interpolation line from two files
router2.dat using awk/perl
10 43.4
20 56.8
30 87.9
:
100 23.8

sender.dat
13 56.9
26 45.9
39 50.0
:
117 59.37

y = ((t-ti)y(i+1) + (t(i+1) - t)yi) / delta_t
# 2  
Old 10-27-2008
Normally, one doesn't use awk or perl to read files in subfolders -- that's usually taken care of by the command line. In perl, it's possible to use the File::Find module (do a man on File::Find). But I find that module confusing and unwieldy, especially when you just want to read all the files in a directory. Better use a find pipeline into a "while read file; do perl .... $file; done" loop, or use xargs to similar effect.

Concerning question #1, do you need to plot the columns from each file separately, or do you need to aggregate them in some way? That is, all from one folder, all together, etc. If it's the latter, it might make more sense to import that data into a, for instance, MySQL database. Otherwise, you'll need to specify the command line with every single file in each group. (Or use the File::Find module with perl).

Concerning 2. You don't "plot" anything with perl/awk. You do that with another tool, such as gplot. If you want to use file2 (sender.dat) together with file1 (router2.dat), you can simply merge them together with sort:
Code:
sort -k 1n,2 file1 file2

The output should go to a temporary file or be piped into awk/perl, which can then do the interprolation. Unfortunately, I cannot interpret your formula for y. Especially the y[i+1] bit. With awk and perl, you can look *behind* a line, but ahead of one. So it would be better if you re-do the formula in terms of i and i-1.
# 3  
Old 11-03-2008
interpolation

hi otheus
sorry for delay.
here is the interpolation equation you asked last time.
the slope is give by: {y(i+1) - y(i)} / {t(i+1) - t(i)}

e(t) is interpolation estimate at time t from (sender1.dat and router2.dat):

e(t) = {(t-t(i))y(i+1) + (t(i+1) - t)y(i)} / {t(i+1) - t(i)}

however, I need also find the interpolate value y, which is between y(i) and y(i+1) at time t.

y = y(i) + {(y(i+1) - y(i)) (t - t(i))} / {t(i+1) - t(i)}

I hope you got my point now. I think I could use awk to calculate these interpolation estimated value at time t.
# 4  
Old 11-03-2008
Interpolation using awk/any script

sorry for delay

Unfortunately, I cannot interpret your formula for y. Especially the y[i+1] bit. With awk and perl, you can look *behind* a line, but ahead of one. So it would be better if you re-do the formula in terms of i and i-1.[/quote]

here is the interpolation equation.
the slope is give by: {y(i+1) - y(i)} / {t(i+1) - t(i)}

e(t) is interpolation estimate at time t: #

e(t) = {(t-t(i))y(i+1) + (t(i+1) - t)y(i)} / {t(i+1) - t(i)}


y = y(i) + {(y(i+1) - y(i)) (t - t(i))} / {t(i+1) - t(i)}

I hope you got my point now.
# 5  
Old 11-03-2008
Terribly sorry, but I don't get it. I don't know what you mean by y(i) or t(i). Maybe you can give an example. Again, we'll have to change this formula to i and i-1 instead of i+1 and i.
# 6  
Old 11-03-2008
sorry for that..
assume that we have sampled value in different time:
file1: router1.dat
t load
60 10.26
120 11.52
180 11.52
240 12.00

where t(1)=60, y(1)=10.26, t(2)=120, y(2)=11.52, etc.

file2: sender1.dat
t estimated value
100 18.18
200 8.33
300 9.21
400 6.99
where t(1)=100, y(1)=18.18, t(2)=200, y(2)=8.33, etc.

in this case, i need to calculate the interpolation value, which is
y = y(i) + {(y(i+1) - y(i)) (t - t(i))} / {t(i+1) - t(i)}
as shown below
y = 10.26 + ({(11.52 - 10.69)}30)/60...

I hope, it doesn't give you any misunderstanding this time. I know, I should make it put an example at the first time...

Quote:
Originally Posted by otheus
Terribly sorry, but I don't get it. I don't know what you mean by y(i) or t(i). Maybe you can give an example. Again, we'll have to change this formula to i and i-1 instead of i+1 and i.
# 7  
Old 11-06-2008
Quote:
Originally Posted by mmoses
Code:
 y = y(i) + {(y(i+1) - y(i)) (t - t(i))} / {t(i+1) - t(i)}

as shown below
Code:
y = 10.26 + ({(11.52 - 10.69)}30)/60...

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; }'


Last edited by otheus; 11-06-2008 at 05:04 AM.. Reason: code fix
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