Aditya
Aditya

Reputation: 1104

awk - syntax error and unterminated regexp

I have this awk program

#! /usr/bin/awk

awk '{num = $1
        for (div = 2; div * div <= num; div++) {
            if (num % div == 0)
                break
        }
        if (num % div == 0)
                printf "Smallest divisor of %d is %d\n", num, div;
        else
                printf "%d is prime\n", num;
}'

when i run this, it gives me this error message

awk: cmd. line:1: ./blabla.awk
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: ./blabla.awk
awk: cmd. line:1:   ^ unterminated regexp

I already check that i use the right interpreter but it still doesn't work.

Upvotes: 0

Views: 859

Answers (2)

muru
muru

Reputation: 4897

That isn't an awk program, that's a shell script containing an awk command. An awk program would look like this:

#! /usr/bin/awk -f

{num = $1
        for (div = 2; div * div <= num; div++) {
            if (num % div == 0)
                break
        }
        if (num % div == 0)
                printf "Smallest divisor of %d is %d\n", num, div;
        else
                printf "%d is prime\n", num;
}

That is, it would contain the awk code directly instead of calling awk.

Upvotes: 3

RavinderSingh13
RavinderSingh13

Reputation: 133538

Though you could use bash or sh shebang like #!/bin/bash etc your awk code should work in it. If you want to stick with awk once then try removing space between shebang and path of awk eg--> #!/usr/bin/awk -f once. Note I am on mobile so didn't test it.

Also remove awk ' from your code, you need not to enclose your code in awk '..........' as per @oguz ismail nice reminder.

Upvotes: 1

Related Questions