Help with awk syntax error problem asking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk syntax error problem asking
# 1  
Old 12-15-2011
Help with awk syntax error problem asking

Input file:
Code:
703
1192
720
1162
316
380
1810
439
1969
874

Desired output file:
Code:
3
3

awk code that I tried:
Code:
[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:
Code:
[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.
# 2  
Old 12-15-2011
You have a semicolon after a closing curly brace.

Code:
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


Last edited by agama; 12-15-2011 at 09:56 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax Error Problem

Hi Below script is throwing an error: repos=root filename=/home/admin/Desktop/authz case $repos in root) root_folder="\" do sed "/$root_folder/a $username = $access" $filename done ;; esac exit 0 Error: ./new1.sh: line 77: syntax error near unexpected token `do' (1 Reply)
Discussion started by: ankur328
1 Replies

2. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

3. Shell Programming and Scripting

awk problem with syntax

awk -v sw="lemons|dogs" 'NR>100 && NR<200 BEGIN { c=split(sw,a,""); } { for (w in a) { if ($0 ~ a) d]++; } } END { for (i in a) { o=o (a"="(d]?d]:0)","); } sub(",*$","",o); print o; }' /home/jahitt/data.txt what am i doing wrong with the above code? im pretty sure the issue is in the... (6 Replies)
Discussion started by: SkySmart
6 Replies

4. Shell Programming and Scripting

awk syntax problem

Hi, I am using this awk command in my shell script : find . -name "*" -ctime -6 | xargs cat | grep -E -v ^fileName\|^\(\) | awk -v DATE="${CURR_DATE}" -v DATE_LOG=$DATE_SYS 'BEGIN {FS=";";OFS=";";CONVFMT="%.9g";OFMT="%.9g"}... (4 Replies)
Discussion started by: abhi1988sri
4 Replies

5. Shell Programming and Scripting

Syntax Problem with awk

Hello, I have perl script,which take some part of data in the file. the below command works fine in normal cmd prompt. `awk '/CDI/ && // && !/Result for/ {print $3 $5 > "final.txt"}' datalist.txt`; `nawk -F"" '{print $2}' finalcdi.txt`; But not working. Please use code tags, thanks. (5 Replies)
Discussion started by: rasingraj
5 Replies

6. Shell Programming and Scripting

Problem with awk syntax

Hi, Below is the code I am using. I am trying to list only those numbers which has a + symbol in it cat num | awk -F"+" '{if (/^$/) { } else {if ( $0 ~ egrep "^+$" ) { if ( $0 ~ grep "+" ) {print $0} } }}' I am getting the following error: awk: 0602-521 There is a... (7 Replies)
Discussion started by: sudvishw
7 Replies

7. Shell Programming and Scripting

Problem with syntax using awk

Hi Guys, When below code is executed in script, I get desired output in output file. awk 'NR >= $start_line && NR <= 3' master_scriptlist.txt > $driver1/scriptlist.txtBut when i replace 3 with a variable end_line=3, I do not get ouput. See code below. Is there any problem with syntax awk... (6 Replies)
Discussion started by: ajincoep
6 Replies

8. Shell Programming and Scripting

remote awk syntax problem

Hi there If i run this command on my Linux box directly, i get the desired result # ipmitool fru | gawk '!NF{f=0}/mb.net0.fru/{f=1}/Product Serial/&&f{print $NF}' 00:AA:4F:A6:A6:C4 however, if i try to run it from a remote server (using SSH) and populating a variable with the result,... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

10. Shell Programming and Scripting

problem with script and syntax error message

Hi I have the following script and have problem debugging the problems. The function of this script is to make sure the entire file is being received (the filesize of a data is not changing after 20 seconds) and start moving the file to another directory. This script should be started every 30mins.... (5 Replies)
Discussion started by: ReV
5 Replies
Login or Register to Ask a Question