Visit Our UNIX and Linux User Community


Averaging segments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Averaging segments
# 8  
Old 09-15-2009
Hey

yeah it works not but everything gets printed into one row. How do I split it up into columns.

---------- Post updated at 02:38 PM ---------- Previous update was at 02:26 PM ----------

Hey maybe I should clarify myself.

So basically there are many rows so I want to the bin average for multiple rows
# 9  
Old 09-15-2009
Hi,

How about this:
Code:
awk '{for(i=3;i<=NF;i+=3) printf "%s ", ($(i-2)+$(i-1)+$i)/3}' file

# 10  
Old 09-15-2009
hey that works. Now I need to scale up to average over a range of 40 numbers


Code:
 awk '{for(i=40;i<=NF;i+=40) printf "%s ", ($(i-39)+$(i-38)$+(i-37)+$(i-36)+$(i-35)+$(i-34)+$(i-33)+$(i-32)+$(i-31)+$(i-30)+$(i-29)+$(i-28)+$(i-27)+$(i-26)+$(i-25)+$(i-24)+$(i-23)+$(i-22)+$(i-21)+$(i-20)+$(i-19)+$(i-18)+$(i-17)+$(i-16)+$(i-15)+$(i-14)+$(i-13)+$(i-12)+$(i-11)+$(i-10)+$(i-9)+$(i-8)+$(i-7)+$(i-6)+$(i-5)+$(i-4)+$(i-3)+$(i-2)+$(i-1)+$i)/40}' file1.txt > file2.txt

i put this in but there is something wrong. hope someone can help. Basically if i have a file with 40 number 1's, the average is not 1 but 5.95.

thanks
# 11  
Old 09-16-2009
For longer range it is easier to iterate through the fields to calculate the sum. And also check if you really have *exactly* 40 1's in your test file.

Code:
awk -v r=40 '{for(i=r;i<=NF;i+=r){for(j=0;j<r;j++){sum+=$(i-j)}printf "%s ", sum/r;sum=0}}' file



---------- Post updated at 07:34 AM ---------- Previous update was at 07:21 AM ----------

Or in a more readable way if you prefer (I do!)

Code:
awk -v r=40 '{
    for(i=r;i<=NF;i+=r){
	for(j=0;j<r;j++){
	    sum+=$(i-j)
	}
	printf "%s ", sum/r
	sum=0
    }
}' file

# 12  
Old 09-16-2009
Hey it works

hey thanks it works

but for some reason it does not treat each row independently

eg.
Code:
 1 1 1 1 1 1 1 1 1 1 1 1 ... n=40
 1 1 1 1 1 1 1 1 1 1 1 1 ... n=40
 1 1 1 1 1 1 1 1 1 1 1 1 ... n=40

The output will look like this
Code:
1 1 1

rather than
Code:
1
1
1


Previous Thread | Next Thread
Test Your Knowledge in Computers #649
Difficulty: Easy
aterm is an rxvt based terminal emulator developed for Afterstep.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting segments

I have queue.txt with the following contents: Queue on node ... description : type : local max message len : 104857600 max queue depth : 5000 queue depth max event : enabled persistent msgs : yes backout... (5 Replies)
Discussion started by: Daniel Gate
5 Replies

2. Shell Programming and Scripting

Grep start and end line of each segments in a file

Cat file1 -------- ---------- SCHEMA.TABLE1 insert------- update----- ------------- ---------- SCHEMA.TABLE2 insert------- update----- ----------- ------------ SCHEMA.TABLE3 insert------- update----- ------------ grep -n SCHEMA > header_file2.txt (2 Replies)
Discussion started by: Veera_V
2 Replies

3. SuSE

TCP segments retransmission

Hi all, I got an application that is running on SUSE Linux. I would like to get some data about the number of TCP segments retransmission on a particular interface. Is there any way I can get that? Thanks, (2 Replies)
Discussion started by: Pouchie1
2 Replies

4. Shell Programming and Scripting

Averaging segments and including the name

Hello, I have a awk line that averages rows. So if my file looks like this: Jack 1 1 1 1 1 1 Joe 1 1 1 1 1 1 Jerry 0 0 0 0 0 0 John 1 1 1 0 0 0 The awk line below skips column 1 and then averaged the rows awk -F'\t' -v r=3... (3 Replies)
Discussion started by: phil_heath
3 Replies

5. UNIX for Dummies Questions & Answers

Averaging

Hello all, I'm trying to perform an averaging procedure which selects a selection of rows, average the corresponding value, selects the next set of rows and average the corresponding values etc. The data below illustrates what I want to do. Given two columns (day and value), I want to... (2 Replies)
Discussion started by: Muhammad Rahiz
2 Replies

6. Programming

C programming - Memory Segments

Can someone tell me how many bytes are allocated for C segments(text,data,heap,stack). (3 Replies)
Discussion started by: nandumishra
3 Replies

7. Shell Programming and Scripting

Compare EDI files by skipping selected Segments

Hi, I wanted to compare EDI files present in Two different Directories which can be related by the file names. While comparing the EDI files i have to skip selected segments such as "ISA" "IEA" and "GS" "GE" since this may have datetime stamp and different "Sender" "Receiver" Qual. and... (3 Replies)
Discussion started by: Sivas
3 Replies

8. Programming

Write into shared memory segments

I have created a shared memory segment (which size is 64 bytes) using shmget, shmat e.t.c and i want to divide it into 2 areas. One area for input data and one area for output? How can i do that? Furthermore, When i have to write my input data into the shared memory segment i want to write... (3 Replies)
Discussion started by: mae4
3 Replies

9. HP-UX

HP-UX Trying to Understand Shared Memory Segments

I am fairly new to HP-UX and trying to get a better understanding of the operating system. While poking around a bit I find myself questioning whether I should be concerned about Shared Memory segments with missing CPID and LPID? For example: ipcs -mp IPC status from /dev/kmem as of Mon Mar... (2 Replies)
Discussion started by: scotbuff
2 Replies

10. HP-UX

Shared Memory segments

Hello.... AIX has a limit of 11 shared memory segments per process, does any one know how many HP have?? If so how do I find that out?? Thanks in advance...... (2 Replies)
Discussion started by: catwomen
2 Replies

Featured Tech Videos