This is my script:
for i in *.locs
do
awk -v start=$(head -n 1 ${i}) -v end=$(tail -n 1 ${i})
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 {
echo "${i}"
print "start =", range_start, "end =", range_end, "mean =", sum / count
}
done
Which gives me this error:
line 15: syntax error near unexpected token `}'
line 15: `}'
But when I first use the awk to generate the variables start and end followed by -f myscript.sh file
I don't get an error:
What am I missing?
Thanks in advance
awkscript you have to store separately asscript.awk, for example, and call withawk ... -f script.awk.awkis notshell.echo "${i}"is ashellcommand. You cannot callechofromawk, just like you couldn't call it fromC, etc. It's not clear what you're trying to do which probably means your approach is wrong - if you tell us what you're trying to do and post some small sample input and expected output, we could help.