niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
awk and simple for loop error
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
Santhan
*nix forums beginner


Joined: 03 Feb 2005
Posts: 5

PostPosted: Fri Feb 04, 2005 2:52 pm    Post subject: Re: awk and simple for loop error Reply with quote

Ed Morton wrote:
Quote:


Santhan wrote:

Hello,

I'm trying to get to grips with awk/nawk, however, a simple for loop
fails. Can anyone tell me why? The script is called testscript:

for ( x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}

snip
You already got your sytnax question answered, but as a point of good
programming style - don't use "continue" or "break" unnecessarily to
change normal control flow through your program. They're just one small
step away from "goto". In your case, write the code as:

for ( x = 1; x <= NF; ++x) {
if (x != 3) {
print x, $x
}
}

It makes the result easier to understand and maintain in future.

Regards,

Ed.

ok, thanks for your help.
Back to top
Santhan
*nix forums beginner


Joined: 03 Feb 2005
Posts: 5

PostPosted: Fri Feb 04, 2005 2:48 pm    Post subject: Re: awk and simple for loop error Reply with quote

Stephane CHAZELAS wrote:
Quote:
2005-02-04, 13:32(+00), Santhan:

Hello,

I'm trying to get to grips with awk/nawk, however, a simple for loop
fails. Can anyone tell me why? The script is called testscript:

for ( x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}

and I when I issue the command:
awk -f testscript testdata
I the error:
awk: syntax error near line 1
awk: bailing out near line 1

[...]

Actions have to be enclosed in braces and follow a condition. If
you ommit the condition, the action will take place for every
record (line).

{
for (x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}
}

Or:

NF > 0 {
for (x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}
}


Thanks it's working and thanks for your help previously.
Back to top
Ed Morton
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1073

PostPosted: Fri Feb 04, 2005 1:31 pm    Post subject: Re: awk and simple for loop error Reply with quote

Santhan wrote:
Quote:
Hello,

I'm trying to get to grips with awk/nawk, however, a simple for loop
fails. Can anyone tell me why? The script is called testscript:

for ( x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}
snip

You already got your sytnax question answered, but as a point of good
programming style - don't use "continue" or "break" unnecessarily to
change normal control flow through your program. They're just one small
step away from "goto". In your case, write the code as:

for ( x = 1; x <= NF; ++x) {
if (x != 3) {
print x, $x
}
}

It makes the result easier to understand and maintain in future.

Regards,

Ed.
Back to top
Marty
*nix forums Guru


Joined: 16 Mar 2005
Posts: 660

PostPosted: Fri Feb 04, 2005 1:23 pm    Post subject: Re: awk and simple for loop error Reply with quote

2005-02-04, 14:17(-00), Robert Bonomi:
[...]
Quote:
Review: the base syntax (using '[[' as a meta-character for 'optional') is:

[[ pattern ]] { action }
[[ [[ pattern ]] { action } ]]
. . .
. . .
. . .


Note the 'syntactic sugar' before, and after, the "action".


Note that the action is also optional.

--
Stéphane
Back to top
Robert Bonomi
*nix forums Guru Wannabe


Joined: 23 Mar 2005
Posts: 175

PostPosted: Fri Feb 04, 2005 1:17 pm    Post subject: Re: awk and simple for loop error Reply with quote

In article <ctvt8s$eif$1$830fa17d@news.demon.co.uk>,
Santhan <sp@bhfshops.org.uk> wrote:
Quote:
Hello,

I'm trying to get to grips with awk/nawk, however, a simple for loop
fails. Can anyone tell me why? The script is called testscript:

for ( x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}

and I when I issue the command:
awk -f testscript testdata
I the error:
awk: syntax error near line 1
awk: bailing out near line 1

when I issue the command:
nawk -f testscript testdata
I get the error:
nawk: syntax error at source line 1
context is
for <<< ( x = 1; x <= NF; ++x) {
nawk: bailing out at source line 1

the file testdata just contains 10 lines with a word on each line.


Thanks for any help,

You need to ask yourself "What is the syntax for an AWK script?"

Review: the base syntax (using '[[' as a meta-character for 'optional') is:

[[ pattern ]] { action }
[[ [[ pattern ]] { action } ]]
. . .
. . .
. . .


Note the 'syntactic sugar' before, and after, the "action".

Your script is "sour", try adding some sugar.
Back to top
Marty
*nix forums Guru


Joined: 16 Mar 2005
Posts: 660

PostPosted: Fri Feb 04, 2005 12:45 pm    Post subject: Re: awk and simple for loop error Reply with quote

2005-02-04, 13:32(+00), Santhan:
Quote:
Hello,

I'm trying to get to grips with awk/nawk, however, a simple for loop
fails. Can anyone tell me why? The script is called testscript:

for ( x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}

and I when I issue the command:
awk -f testscript testdata
I the error:
awk: syntax error near line 1
awk: bailing out near line 1
[...]


Actions have to be enclosed in braces and follow a condition. If
you ommit the condition, the action will take place for every
record (line).

{
for (x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}
}

Or:

NF > 0 {
for (x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}
}

--
Stéphane
Back to top
Santhan
*nix forums beginner


Joined: 03 Feb 2005
Posts: 5

PostPosted: Fri Feb 04, 2005 12:32 pm    Post subject: awk and simple for loop error Reply with quote

Hello,

I'm trying to get to grips with awk/nawk, however, a simple for loop
fails. Can anyone tell me why? The script is called testscript:

for ( x = 1; x <= NF; ++x) {
if (x == 3)
continue
print x, $x
}

and I when I issue the command:
awk -f testscript testdata
I the error:
awk: syntax error near line 1
awk: bailing out near line 1

when I issue the command:
nawk -f testscript testdata
I get the error:
nawk: syntax error at source line 1
context is
Quote:
for <<< ( x = 1; x <= NF; ++x) {
nawk: bailing out at source line 1


the file testdata just contains 10 lines with a word on each line.


Thanks for any help,
Santhan
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 7:55 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Postfix + MySQL error: very strange variable %s iWarior Postfix 0 Mon Aug 25, 2008 2:01 pm
No new posts ** Postfix error on console every minute or so ** ?? drywash Postfix 0 Fri Jul 04, 2008 8:49 pm
No new posts Postfix error bounce diwash Postfix 0 Fri Mar 28, 2008 3:37 am
No new posts I am getting following error in Aix 5.3 rockcharles1 AIX 0 Tue Aug 28, 2007 11:06 pm
No new posts mail forwarding loop killersushi Postfix 0 Tue Jan 23, 2007 3:43 am

Free mobile phone's stuffs | Arab Girl | Apply for Credit Card | Directory of Directories | Loan
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2879s ][ Queries: 20 (0.1971s) ][ GZIP on - Debug on ]