|
|
|
|
|
|
| Author |
Message |
Santhan *nix forums beginner
Joined: 03 Feb 2005
Posts: 5
|
Posted: Fri Feb 04, 2005 2:52 pm Post subject:
Re: awk and simple for loop error
|
|
|
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
|
Posted: Fri Feb 04, 2005 2:48 pm Post subject:
Re: awk and simple for loop error
|
|
|
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
|
Posted: Fri Feb 04, 2005 1:31 pm Post subject:
Re: awk and simple for loop error
|
|
|
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
|
Posted: Fri Feb 04, 2005 1:23 pm Post subject:
Re: awk and simple for loop error
|
|
|
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
|
Posted: Fri Feb 04, 2005 1:17 pm Post subject:
Re: awk and simple for loop error
|
|
|
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
|
Posted: Fri Feb 04, 2005 12:45 pm Post subject:
Re: awk and simple for loop error
|
|
|
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
|
Posted: Fri Feb 04, 2005 12:32 pm Post subject:
awk and simple for loop error
|
|
|
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 |
|
 |
|
|
The time now is Thu Jan 08, 2009 7:55 pm | All times are GMT
|
|
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
|
|