Awk Switch Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this awk syntax error near unexpected token (' site About Us Learn more about Stack Overflow the company Business Learn awk syntax error near line 1 more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x awk syntax error at source line 1 Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up switch/case
Awk Syntax Error At Source Line 1 Context Is
doesn't work in awk up vote 8 down vote favorite I am writing a simple awk in redhat linux,but found switch/case doesn't work for me. I searched on web, but didn't find a solution. The following is my code: BEGIN { foo = 1; switch (foo) { case 3: print "x"; break; case 2: print "y" ; break; case 1: print "z" ; break; awk if syntax error default: print "default" ; } } the awk I am running is GNU Awk 3.1.5. I got the following err: awk -f test.awk awk: test.awk:3: switch (foo) { awk: test.awk:3: ^ syntax error awk: test.awk:5: case 3: awk: test.awk:5: ^ syntax error awk: test.awk:8: case 2: awk: test.awk:8: ^ syntax error awk: test.awk:11: case 1: awk: test.awk:11: ^ syntax error awk: test.awk:14: default: awk: test.awk:14: ^ syntax error can anybody please help me out? thank you! linux awk share|improve this question edited Jul 9 '12 at 19:50 Dennis Williamson 167k42235305 asked Jul 9 '12 at 19:41 Gary 1,06161936 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote The GAWK manual says: 6.4.5 The switch Statement NOTE: This subsection describes an experimental feature added in gawk 3.1.3. It is not enabled by default. To enable it, use the ‘--enable-switch’ option to configure when gawk is being configured and built. See Section B.2.2 [Additional Configuration Options], page 269, for more information. The switch statement allows the evaluation of an expression and the execution of statements based on a case match. Case statements are checked for a matc
the case/switch statement of the shell and C, in awk? How would one write it? Would one merely use: Aharon> if ... else if ... Also, if as in C, unlike the shell, I wanted to only awk switch columns do the 'switch' amongst integer choices, is there a better way [more smart looking or more
Awk Switch Regex
CPU efficient [or am I already as efficient as I can get anyway]]? Would it involve arrays? -- http://www.*-*-*.com/ :restore .com. Wed, 10 Dec
Awk If Else If
2003 13:05:06 GMT Rafal M. Sulejma#2 / 12 how to do "case/switch" in awk Quote:> case/switch statement of the shell and C, in awk? How would one write > it? Would one merely use: > Aharon> if ... else http://stackoverflow.com/questions/11401979/switch-case-doesnt-work-in-awk if ... In awk case is the outermost control structure. One used to explain it's inner workings using while(getline()){ switch (line){ pattern0 { action0 } ... patternN { actionN } Quote:} Heh, that's one of the awk gotchas. The best way is (at least it was in many cases) to rethink problem http://computer-programming-forum.com/11-awk/4e0aff7804ac7f78.htm to move `switch'ing to natural /pattern/{action} schema. In other cases you have (IMHO) to use `if's... -- Written at: # . . . . # # . . # . . # # . . Thu, 11 Dec 2003 22:25:48 GMT Michael Mau#3 / 12 how to do "case/switch" in awk Quote: > Hello awk professionals, I was wondering how to enjoy the ease of the > case/switch statement of the shell and C, in awk? How would one write > it? Would one merely use: > Aharon> if ... else if ... > Also, if as in C, unlike the shell, I wanted to only do the 'switch' amongst > integer choices, is there a better way [more smart looking or more CPU > efficient [or am I already as efficient as I can get anyway]]? Would > it involve arrays? If you have a long "if ... else if ..." serpent to just map some values, you can better use arrays. Instead of: { if(1==foo) bar = 27 else if(2==foo) bar = 53 else if("baz"==foo) bar = "bla" ... Quote:} With an array, you initialize it in the BEGIN section: BEGIN \ { BAR[1] = 27 BAR[2] = 53 BAR["baz"] = "bla" ... Quote:} ... and can use it like this: bar = BAR[foo] or if you might receive some unknown
from GoogleSign inHidden fieldsSearch for groups or messages
to configure when gawk is being configured and built. See Additional Configuration Options, for more information. The switch statement allows the evaluation of an expression and the execution of statements based on a case match. Case statements are checked for a match in the order they are defined. If no suitable case is found, the default section is executed, if supplied. Each case contains a single constant, be it numeric, string, or regexp. The switch expression is evaluated, and then each case's constant is compared against the result in turn. The type of constant determines the comparison: numeric or string do the usual comparisons. A regexp constant does a regular expression match against the string value of the original expression. The general form of the switch statement looks like this: switch (expression) { case value or regular expression: case-body default: default-body } Control flow in the switch statement works as it does in C. Once a match to a given case is made, case statement bodies are executed until a break, continue, next, nextfile or exit is encountered, or the end of the switch statement itself. For example: switch (NR * 2 + 1) { case 3: case "11": print NR - 1 break case /2[[:digit:]]+/: print NR default: print NR + 1 case -1: print NR * -1 } Note that if none of the statements specified above halt execution of a matched case statement, execution falls through to the next case until execution halts. In the above example, for any case value starting with `2' followed by one or more digits, the print statement is executed and then falls through into the default section, executing its print statement. In turn, the −1 case will also be executed since the default does not halt execution.