Help needed!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed!
# 1  
Old 01-25-2013
Help needed!

Hi,

I need to calculate average of a column based on row values of another file.

File 1:

Code:
ID     start    end   
1      2         5
1      8         10
2      3         5
2      7         11
...

File 2:
Code:
ID     position    y
1       1            0
1       2            2
1       3            2
1       4            0
1       5            1
1       6            0
1       7            0
1       8            1
1       9            1
1       10          1
1       11          1
1       12          2
...

output:

Code:
ID    start    end     mean.y
1      2        5         1.2
1      8        10       1
...

Basically, the added column "mean.y" is average "y" value of row 2 to 5 in file 2, i.e., 1.2=(2+2+0+1)/4.

Thanks a lot!

Last edited by Scrutinizer; 01-25-2013 at 02:01 AM.. Reason: code tags
# 2  
Old 01-25-2013
Here is one way of doing it:
Code:
while read id st en
do
  awk -v I=$id -v S=$st -v E=$en 'BEGIN{c=0;m=0;}$1==I&&NR>=S&&NR<=E{++c;m+=$NF;}END{if(c!=0) printf "%d %d %d %.1f\n",I,S,E,m/c;}' file2
done < file1

# 3  
Old 01-25-2013
Thank you so much!

There is a little bit problem. Could you please take a look?

Code:
 while read id start end
 do
 awk -v I=$id -v S=$start -v E=$end 'BEGIN{c=0; m=0;}$1==I&&NR>=S&&NR<=E{++c;m+=$NF;}END{if(c!=0) printf "%d %d %d %.1f\n",I,S,E,m/c;}' file2.txt
 done < file1.txt

Code:
awk: cmd. line:1:
' in expression1: ^ invalid char '
1 2 5 1.0
1 8 10 0.7


##############
Can you please check why the average of rows 2 to 5 is 1.0, rather than 1.2?

Thank you very much!

Last edited by Scrutinizer; 01-25-2013 at 02:02 AM.. Reason: code tags
# 4  
Old 01-25-2013
Oh I missed to skip the headers in your files. Try this modified code:-
Code:
while read id st en
do
[[ $id =~ "^[0-9]+$" ]] && awk -v I=$id -v S=$st -v E=$en 'BEGIN{c=0;m=0;}$1==I&&NR>=S+1&&NR<=E+1{++c;m+=$NF;}END{if(c!=0) printf "%d %d %d %.1f\n",I,S,E,m/c;}' file2
done < file1

# 5  
Old 01-25-2013
Thank you so much! bipinajith.

It works great!

However, It's totally my fault that I did not explain it correctly in my post.

Actully, what I really wanted was to average rows that have "pos" values (in file2) between "start" and "end".

For example, file 1 is still

Code:
id start end 
1 2 5
1 8 10
2 3 5
2 7 11

file 2

Code:
id pos y
1 1 0
1 3 2
1 5 1
1 6 0
1 7 0
1 8 1
1 9 1
1 10 1
1 11 1
1 12 2


output:
Code:
id start end mean.y
1 2 5 0.75
1 8 10 1.0
2 3 5
2 7 11

where 0.75=(2+1)/4 <- because start=2 and end=5, so only rows with "pos" = 3 and 5 are averaged.

Thank you so much!

---------- Post updated at 01:06 AM ---------- Previous update was at 12:57 AM ----------

Hi, bipinajith.

I think your code works great! And IT IS what I wanted! Please ignore the last post!

Thanks a lot!

Last edited by Scrutinizer; 01-25-2013 at 02:03 AM.. Reason: code tags
This User Gave Thanks to jackken007 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed

First of all, let me state that I am a windows admin. I have a windows share mounted to /mnt/server I need a script that will either login as sudo or perform commands with sudo rights. I need the script to copy all of the users /home folders to the mounted windows share. Now If I can... (2 Replies)
Discussion started by: EricM
2 Replies

2. Shell Programming and Scripting

Help Needed

please reply for this https://www.unix.com/shell-programming-scripting/111493-cutting-lines.html its really urgent (1 Reply)
Discussion started by: jojo123
1 Replies

3. Shell Programming and Scripting

help needed...

Guys, There is a file where there are 1000s of records. In the file if some condition satisfies in a certain TAB record (TAB would be first 3 digits of a certain record) then move TAB and all the records (or lines) after TAB to new_file, until another TAB record is encountered in the same... (1 Reply)
Discussion started by: Prat007
1 Replies

4. Shell Programming and Scripting

Help needed ....

Hi... I have a folder /home/data ;where some files are present. aaa_asas.txt bbb_xxx.txt ccc_xsxas.txt ddd_sa2esa.txt ------ Also I have a file which is as follows.(/home/file1) cat /home/file1 aaa you bbb are ccc very ddd good -------- now I want to rename all the files in the folder... (7 Replies)
Discussion started by: newbee1
7 Replies

5. UNIX for Dummies Questions & Answers

Help needed please.

i've been given an assignment to Write a system utility called recycle that satisfies the following requirements as they might be displayed in a UNIX/Linux man page: NAME recycle - stores files in a recycle bin SYNOPSIS recycle ... DESCRIPTION Recycle is a replacement for the... (3 Replies)
Discussion started by: jerryboy78
3 Replies

6. UNIX for Dummies Questions & Answers

little help needed..

hi everyone i'm a noob trying to learn unix language.. but seems like i got no leads on how to start.. i'm playing with the 'ps' command.. i'm trying to show the pid, ppid, username, command, cpu utilization (in desc order), process start time and process status.. all in a command.. am i able... (3 Replies)
Discussion started by: hilofat
3 Replies

7. AIX

Little help needed.

Hello, I am quite new to AIX, but have Linux experience. Iam facing a peoblem with AIX 5.2 running on a 43p Model 150 (RS6000). I tried everyting and i cant have the network to run properly. :confused: /etc/hosts looks like this: 127.0.0.1 loopback localhost 192.168.XXX.XXX... (5 Replies)
Discussion started by: Netghost
5 Replies

8. UNIX for Dummies Questions & Answers

Help needed

HI can any one help me with the appropriate answers for the below: 1.Enter an # before a command and press .what do you see,and how do you think you can take advantage of the behaviour? 2.Is tar -cvfb20foo.tar*.c legitimate or not.will this command work without the - symbol? 3.The command... (1 Reply)
Discussion started by: akhil1460
1 Replies

9. UNIX for Dummies Questions & Answers

Help needed

Hello I am a newbie and want to learn unix . Does unix and linux are one and same. I have red hat linux cd but i want to take advice from some one wheather unix and linux are same. If not ,where i'll get a Unix os setup and how i'll install it. If linux would do then how should... (3 Replies)
Discussion started by: hunter87
3 Replies

10. Shell Programming and Scripting

Help is needed

Hi I'm trying to print a directories struct tree that will look like this: A _a _b _B __c __d __C ___e B _a _b I'm doing a recursion, but how can I know how much space is needed before printing after the recursion? (3 Replies)
Discussion started by: abcde
3 Replies
Login or Register to Ask a Question