2

I have a file of data for 1024 time steps that looks like this:

1 0.240112  0.0610352 1.00659  
2 0.240112  0.0610352 1.00659
...
1024    0.237366  0.0576172 1.00189   

I want to do two things --

  1. Decrease the number of rows by a factor of N, by averaging over the values of every N rows. So, e.g., for N=16, I would be left with 1024/16=64 rows of data
  2. Average over an exponentially increasing number of rows. E.g., if I want to average over every 2^N rows (from 1-2, 3-4, 5-8, 9-16, ...). In this case, I would be left with log_2(1024)=10 rows.

Can I do this using grepor awkor some such command?

0

First task:

awk '
BEGIN {
    N = 16;
}
{
    for(i = 1; i <= NF; i++) {
        arr[i] += $i;   
    }
}
NR % N == 0 {
    for(i = 1; i <= NF; i++) {
        printf "%s ", arr[i] / N;
    }
    print "";
    delete arr;

}' input.txt

Second task:

awk '
BEGIN {
    N = 2;
}
{
    for(i = 1; i <= NF; i++) {
        arr[i] += $i;   
    }
}
NR % N == 0 {
    for(i = 1; i <= NF; i++) {
        printf "%s ", arr[i] / (N - M);
    }
    print "";
    delete arr;

    M = N;
    N *= 2;
}' input.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.