Awk Switch Statement 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 site About Us Learn more about Stack Overflow awk syntax error near line 1 awk bailing out near line 1 the company Business Learn more about hiring developers or posting ads with us Stack Overflow
Awk Syntax Error Near Unexpected Token ('
Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 awk syntax error at source line 1 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Errors when executing switch/case in awk command in Ubuntu and Mac up vote 0 down vote favorite I have
Awk Syntax Error At Source Line 1 Context Is
a very strange problem when executing following code: awk '{ foo = 1; switch (foo) { case 1: i=i+1; break; } }' ./tcpheader.txt Getting following error: awk: syntax error at source line 1 context is { foo = 1; switch (foo) >>> { <<< awk: illegal statement at source line 1 awk: illegal statement at source line 1 Any idea what's the problem ? Tested on Mac: awk --version Output: awk version awk if syntax error 20070501 Tested on Ubuntu: awk -W version Output: mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan compiled limits: max NF 32767 sprintf buffer 2040 bash awk mawk share|improve this question edited Jun 20 '15 at 9:47 asked Jun 20 '15 at 9:05 Arsi 115 2 Please add name and version of your awk to your question. The switch statement is a gawk-specific feature. –Cyrus Jun 20 '15 at 9:33 @Cyrus I added the versions info. –Arsi Jun 20 '15 at 9:42 Have a look at this: stackoverflow.com/questions/11401979/… –Rakholiya Jenish Jun 20 '15 at 9:42 @RakholiyaJenish I saw it, --enable-switch didn't help. –Arsi Jun 20 '15 at 9:43 1 I think I have to install gawk instead of default awk ubuntu and mac awk version ... –Arsi Jun 20 '15 at 9:44 | show 1 more comment 1 Answer 1 active oldest votes up vote 1 down vote accepted Installing gawk solved my problem: sudo apt-get install gawk in mac: sudo port install gawk share|improve this answer answered Jun 20 '15 at 9:46 Arsi 115 right, switch is a gawk extension as clearly stated in the documentation gnu.org/software/gawk/manual/gawk.html#Switch-Statement –Ed Morton Jun 21 '15 at 13:51 add a comment| Your Answer draft saved draft discarded S
allows the evaluation of an expression and the execution of statements based on a case match. Case statements are checked for a
Awk Switch Statement Example
match in the order they are defined. If no suitable case
Awk Switch Regex
is found, the default section is executed, if supplied. Each case contains a single constant, be it awk if else if 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 http://stackoverflow.com/questions/30952237/errors-when-executing-switch-case-in-awk-command-in-ubuntu-and-mac 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 https://www.gnu.org/s/gawk/manual/html_node/Switch-Statement.html in C. Once a match to a given case is made, the case statement bodies execute until a break, continue, next, nextfile, or exit is encountered, or the end of the switch statement itself. For example: while ((c = getopt(ARGC, ARGV, "aksx")) != -1) { switch (c) { case "a": # report size of all files all_files = TRUE; break case "k": BLOCK_SIZE = 1024 # 1K block size break case "s": # do sums only sum_only = TRUE break case "x": # don't cross filesystems fts_flags = or(fts_flags, FTS_XDEV) break case "?": default: usage() break } } Note that if none of the statements specified here halt execution of a matched case statement, execution falls through to the next case until execution halts. In this example, the case for "?" falls through to the default case, which is to call a function named usage(). (The getopt() function being called here is described in Getopt Function.) Next: Break Statement, Previous: For Statement, Up: Statements [Contents][Index]
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages http://www.unix.com/shell-programming-and-scripting/147870-something-wrong-switch-case-statement.html here. Search Forums Show Threads Show Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... linux operating commands and unix operating http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.awk/2010-01/msg00174.html commands Something is wrong with this switch( case statement. Shell Programming and Scripting Tags bash, flow control, menu, select case, switch Thread syntax error Tools Search this Thread Display Modes #1 11-06-2010 snakemasterAK Registered User Join Date: Nov 2010 Last Activity: 6 November 2010, 5:41 PM EDT Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts Something is wrong with this switch( case statement. I started writing a script to save the files from a camera awk syntax error I got the other day, which mounts in /Volumes , and I got into it and started building this menu. The only problem is that the switch case is coming up as a syntax error at the parenthesis after a case. Here is the code: Code: while : do clear echo "-----Menu for Camera Save Script-----" echo " Main Menu " echo "-------------------------------------" echo "[1] Save to dir: Todays date/time" echo "[2] Save files custom dir: " echo "[5] Exit/Stop" echo "=======================" echo -n "Enter your menu choice [1,2,5]: " read yourch case "$yourch" in 1) mkdir $HOME/Pictures/`date +%d_%m_%H%M`; mv /Volumes/SpyCam/_REC/100MEDIA/* $HOME/Pictures/`date +%d_%m_%H%M`; ;; 2) echo "Enter Name of New Dir: "; read `newdir`; mkdir $HOME/Pictures/$newdir; mv /Volumes/SpyCam/_REC/100MEDIA/* $HOME/Pictures/$newdir; ;; 5) exit 0; ;; *) echo "No try 1, 2 or 5."; echo "Press a key. . ."; read; ;; esac done As you can see, it displays the menu, and once it does that, it waits for two options, one for an auto named directory with t
code. Ah, I see you did that. The next thing is to double check that root's search path includes the current directory. Try invoking as ./gawk ... switch/case works for me when I configure gawk correctly. Arnold In article