Help with awk syntax error problem asking

Input file:

703
1192
720
1162
316
380
1810
439
1969
874

Desired output file:

3
3

awk code that I tried:

[home@perl_beginner]awk '{if($1>=300 && $1<500){x++};else if ($1>=500 && $1<1000){y++} }END{print x"\n"y}' input_file.txt
awk: {if($1>=300 && $1<500){x++};else if ($1>=500 && $1<1000){y++} }END{print x"\n"y}
awk:                             ^ syntax error

My awk code work fine if I count them separately:

[home@perl_beginner]$ awk '{if($1>=300 && $1<500){x++};}END{print x}' input_file.txt 
3
[home@perl_beginner]$ awk '{if($1>=500 && $1<1000){x++};}END{print x}' input_file.txt 
3

My main purpose just wanna to combine the above two command into one command. I was thinking used "if...else if" condition.
Thanks for any advice.

You have a semicolon after a closing curly brace.

awk '
{
    if( $1>=300 && $1<500)
    {
        x++;                # semicolon needs to be inside the brackets.
    }
    else
    if ($1>=500 && $1<1000)
    {
        y++;
     }
}
END {
    print x"\n"y
}' input_file.txt
1 Like