0

I had a problem that was resolved in a previous post:

But because I had too many files it was not practical to do an awk on every file and then use a second script to get the output I wanted.

Here are some examples of my files:

3 
10 
23
.
.
.
720
810
980

And the script was used to see where the numbers from the first file fell in this other file:

2 0.004
4 0.003
6 0.034
. 
.
.
996 0.01
998 0.02
1000 0.23

After that range was located, the mean values of the second column in the second file was estimated.

Here are the scripts:

awk -v start=$(head -n 1 file1) -v end=$(tail -n 1 file1) -f script file2

and

BEGIN {
        sum = 0;
        count = 0;
        range_start = -1;
        range_end = -1;
    }
    {
        irow = int($1)
        ival = $2 + 0.0
        if (irow >= start && end >= irow) {
                if (range_start == -1) {
                    range_start = NR;
                }
                sum = sum + ival;
                count++;
            }
        else if (irow > end) {
                if (range_end == -1) {
                    range_end = NR - 1;
                }
            }
    }
    END {
        print "start =", range_start, "end =", range_end, "mean =", sum / count
    }

How could I make a loop so that the mean for every file was estimated. My desired output would be something like this:

Name_of_file
start = number , end = number , mean = number

Thanks in advance.

1
  • GNU awk version 4 has BEGINFILE and ENDFILE special patterns, that might be useful. What you should do is reduce your problem to your index file and a couple of your other files, and show your desired output: stackoverflow.com/help/mcve Commented Mar 3, 2014 at 20:33

1 Answer 1

2

.. wrap it in a loop?

for f in <files>; do 
    echo "$f";
    awk -v start=$(head -n 1 "$f") -v end=$(tail -n 1 "$f") -f script file2;
done

Personally I would suggest combining them on one line (so that your results are block-data as opposed to file names on different lines from their results -- in that case replace echo "$f" with echo -n "$f " (to not add the newline).

EDIT: Since I suppose you're new to the syntax, <files> can either be a list of files (file1 file2 file 3), a list of files as generated by a glob (file*, files/data_*.txt, whatever), or a list of files generated by a command ( $(find files/ -name 'data' -type f), etc).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.