0

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

4
  • What cyon replied to your previous question is an awk script you have to store separately as script.awk, for example, and call with awk ... -f script.awk. Commented Mar 3, 2014 at 15:46
  • But because I had so many files and wanted to add that echo "${i}" before printing the result so I could know the result for each file Commented Mar 3, 2014 at 15:51
  • 1
    awk is not shell. echo "${i}" is a shell command. You cannot call echo from awk, just like you couldn't call it from C, 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. Commented Mar 3, 2014 at 16:18
  • Thank you for that Ed Morton. Here is a previous post where I got this script (stackoverflow.com/questions/22145261/…) so you can see my problem. Commented Mar 3, 2014 at 16:29

2 Answers 2

2

You need to either quote the entire awk script, or escape the dollar signs so that the shell does not expand them as positional parameters before awk is called. (Adding single quotes takes care of the other problem, which is that without a line continuation character, the awk command itself ends at the end of the line and the rest of the script is parsed as incorrect bash code):

awk -v start=$(head -n 1 ${i}) -v end=$(tail -n 1 ${i}) '

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

5 Comments

I added the commas but I am still getting an error. I put the second comma in the line before the done
@JM88 Those are not commas, they are single quote characters.
Yes, sorry, I meant single quotes. When I used single quotes I got an error.
@JM88 my crystal ball says it's probably related to the echo "${i}" shell command in your END section. You really need to just update your question to tell us what you're trying to do so we can help you do it right.
I created this new post, explaining it again. I hope you can understand my problem now. Thanks in advance. stackoverflow.com/questions/22152486/adding-a-loop-in-awk
0

Characters within your awk program are being interpreted by the shell. You can pass your program as a command-line argument to awk, but it must be enclosed in 'single-quotes' to prevent the shell from interpreting it.

1 Comment

Where exactly do I need to put the quotes?

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.