Awk Switch Case 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 the company Business Learn more about hiring developers or posting
Awk Syntax Error Near Line 1 Awk Bailing Out Near Line 1
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join awk syntax error near unexpected token (' 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
Awk Syntax Error At Source Line 1
a minute: Sign up Errors when executing switch/case in awk command in Ubuntu and Mac up vote 0 down vote favorite I have a very strange problem when executing following code: awk '{ foo = 1; switch (foo) { case 1: awk syntax error at source line 1 context is 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 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 awk if syntax error 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 Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions
allows the evaluation of an expression and the execution of statements based on a case match. Case statements are checked for a
Awk Switch Case 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 here. Search Forums Show Threads Show Posts Tag Search Advanced Search http://www.unix.com/shell-programming-and-scripting/147870-something-wrong-switch-case-statement.html Unanswered Threads Find All Thanked Posts Go to Page... unix and linux https://www-zeuthen.desy.de/unix/unixguide/infohtml/gawk/gawk.html commands - unix shell scripting Something is wrong with this switch( case statement. Shell Programming and Scripting Tags bash, flow control, menu, select case, switch Thread Tools Search this Thread Display Modes #1 11-06-2010 snakemasterAK Registered User Join Date: Nov 2010 Last Activity: 6 November syntax error 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 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 awk 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 the date and time, or one for a custom named directory. It won't work though. it says there is an unexpected parentheses at 1). Thanks for anyone who tries to figure this out. oh, also, I am executing within bash. Last edited by Franklin52; 11-06-2010 at 07:13 AM.. Reason: Please use code tags Remove advertisements Sponsored Links snakemasterAK View Public Profile Find all posts by snakemasterAK #2 11-0
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009 Free Software Foundation, Inc. This is Edition 3 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 3.1.7 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with the Invariant Sections being “GNU General Public License”, the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled “GNU Free Documentation License”. “A GNU Manual” “You have the freedom to copy and modify this GNU manual. Buying copies from the FSF supports it in developing GNU and promoting software freedom.” Foreword: Some nice words about this Web page. Preface: What this Web page is about; brief history and acknowledgments. Getting Started: A basic introduction to using awk. How to run an awk program. Command-line syntax. Regexp: All about matching things using regular expressions. Reading Files: How to read files and manipulate fields. Printing: How to print using awk. Describes the endfile 6 and endfile 5 statements. Also describes redirection of output. Expressions: Expressions are the basic building blocks of statements. Patterns and Actions: Overviews of patterns and actions. Arrays: The description and use of arrays. Also includes array-oriented control statements. Functions: Built-in and user-defined functions. Internationalization: Getting gawk to speak your language. Advanced Features: Stuff for advanced users, specific to gawk. Invoking Gawk: How to run gawk. Library Functions: A Library of awk Functions. Sample Programs: Many awk programs with complete explanations. Language History: The evolution of the awk language. Installation: Installing gawk under various operating systems. Notes: Notes about gawk extensions and possible future work. Basic Concepts: A very quick introduction to programming concepts. Glossary: An explanation of some unfamiliar terms. Copying: Your right to copy and distribute gawk. GNU Free Documentation License: The license for this Web page. Index: Concept and Variable Index. History: The history of gawk and awk. Names: What name to use to find awk. This Manual: Using this Web page. Includes sample input files that you can use. Conventions: Typographical Conventions. Manual History: Brief history of the GNU pr