Adding a loop in awk

2019-09-07 01:20发布

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条回答
叼着烟拽天下
2楼-- · 2019-09-07 01:48

.. 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).

查看更多
登录 后发表回答