The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 08:16 AM
awk syntax error orahi001 UNIX for Dummies Questions & Answers 0 03-17-2008 06:35 AM
syntax error bkan77 Shell Programming and Scripting 2 07-30-2007 01:26 PM
I got error like...syntax error on line 1, teletype koti_rama UNIX for Advanced & Expert Users 2 07-07-2007 04:35 PM
Syntax error.. livetaurean19 Shell Programming and Scripting 0 06-14-2005 01:23 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-17-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
awk syntax error

Code:
mVar=0

count=`awk -F, '( ( $2 ~ /^GIVEUP$/ && $3 ~ /^NEW$/ ) || ( $2 ~ /^SPLIT$/ && $3 ~ /^NEW$/ ) || ( $2 ~ /^OPTION$/ && $3 ~ /^NEW$/ ) || ( $2 ~ /^OPTIONSPLIT$/ && $3 ~ /^NEW$/ ) ) { count++ } END { print count }' myCSV.csv
    myVar=`expr $myVar + $count`
Can I do this? I get a syntax error.
It works if I remove the 2nd two pairs of "or" operators.
Reply With Quote
Forum Sponsor
  #2  
Old 06-17-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
I didn't find the patience to count all the different types of brackets (I presume a miscount of them is causing the error), but even if the statement would work it would be pretty bad style (read: nearly unreadable for anybody else) so take the malfunction as a stroke of luck.

It is far easier to read to do something (only the skeleton) like:

Code:
BEGIN {
    count=0;
}

{
if( $2 ~ /^GIVEUP$/ && $3 ~ /^NEW$/ )
     count ++;

if( $2 ~ /^SPLIT$/ && $3 ~ /^NEW$/ )
     count ++;

if( ....

}
END {
     print count;
}
This would make clear at first glance what you are doing. The times of "if it was hard to write it should be hard to read (and even harder to understand)" are over by now.

bakunin
Reply With Quote
  #3  
Old 06-18-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
The OP's obfuscated code works fine for me (GNU awk).... my hunch is the omission of the closing backtick.....

Cheers
ZB
Reply With Quote
  #4  
Old 06-19-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Code can be simplified...
Code:
count=$(awk -F, '$2 ~ /^(GIVEUP|SPLIT|OPTION|OPTIONSPLIT)$/ && $3 == "NEW" { count++ } 
                 END { print count }' myCSV.csv)
Reply With Quote
  #5  
Old 06-20-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
very appreciative

ah, thanks guys.
i'm always interested in writing better, easier to understand code, and this post definitely helped out in that department

Last edited by yongho; 06-20-2005 at 07:19 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0