Grep 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 syntax error near unexpected token (' bash about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges grep regex Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each
Grep Recursive
other. Join them; it only takes a minute: Sign up Why do I get unexpected token? [closed] up vote 2 down vote favorite I am trying to search for .c, .cpp or .h files in a text file
Man Grep
contains list of files. However, when I have problems when I ran: $ grep -sr ^.*\.(c|cpp|h)$ . bash: syntax error near unexpected token `(' regex linux shell grep share|improve this question edited Oct 15 '12 at 17:01 Ωmega 21.8k1561117 asked Oct 15 '12 at 16:22 0x90 13k1876155 closed as off topic by Ignacio Vazquez-Abrams, BNL, William Pursell, Zuul, cadrell0 Oct 15 '12 at 17:29 Questions on Stack Overflow are expected to relate to programming within the scope defined grep command by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question. 1 Because ( has a special meaning in bash. You need to escape it. –Álvaro González Oct 15 '12 at 16:24 @ÁlvaroG.Vicario - Not to escape, that would be \(, but put the regex pattern to quotes... –Ωmega Oct 15 '12 at 17:00 Escaping would work as well, however. –chepner Oct 15 '12 at 18:16 $ grep -sr "^.*\.(c|cpp|h)$" . works –0x90 Feb 20 '15 at 19:56 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted I don't have a Unix box to test but: grep -sr '^.*\.(c|cpp|h)$' . ... should at least not trigger a syntax error. share|improve this answer answered Oct 15 '12 at 16:25 Álvaro González 80.9k20133199 add a comment| up vote 1 down vote You should be good with grep -sr '\.(c|cpp|h)$' . Regex pattern should be in quotes There is no need to include ^.* part in the pattern share|improve this answer answered Oct 15 '12 at 16:56 Ωmega 21.8k1561117 add a comment| Not the answer you're looking for? Browse other questions tagged r
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
Regex Tester
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x 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 how to write this grep regex up vote 2 down vote favorite if [ '`echo "$url" | grep http://stackoverflow.com/questions/12899810/why-do-i-get-unexpected-token (\.tar\.gz|\.tar\.bz2|\.zip|\.rar|\.7z)$`' ] ; then syntax error, I just want to check file extension. regex shell grep share|improve this question asked Dec 24 '10 at 16:51 guilin 桂林 5,4641764112 What is the syntax error? –Davidann Dec 24 '10 at 17:10 As shown, you shouldn't be getting an error, but it won't do what you intend. –Dennis Williamson Dec 24 '10 at 17:45 @Dennis He will be getting http://stackoverflow.com/questions/4527395/how-to-write-this-grep-regex an error, as mentioned in my answer. –marcog Dec 24 '10 at 17:56 2 @Marcog: no, he won't be getting a syntax error. if [ '$(echo "$url" | grep (\.tar\.gz|\.tar\.bz2|\.zip|\.rar|\.7z)$)' ] ; then echo Hi; else echo Lo; fi echoes Hi regardless of the value of $url. (I changed backticks to $(...) to get the code echoed as code in the comment; you get the same result with backticks as in the original.) –Jonathan Leffler Dec 24 '10 at 18:30 @Jonathan You are correct. I was only executing the grep, so missed that part entirely. –marcog Dec 24 '10 at 18:36 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted First of all, you need to remove the '' as otherwise the test is just a string that always evaluates to true. You need to put the regex in quotes as parentheses are interpreted by bash. You also need to use egrep (equivalent to grep -E). if [ `echo "$url" | egrep "(\.tar\.gz|\.tar\.bz2|\.zip|\.rar|\.7z)$"` ] ; then You can also shorten the regex by factoring the . out of the group and grouping tar.{gz,bz2}: if [ `echo "$url" | egrep "\.(tar\.(gz|bz2)|zip|rar|7z)$"` ] ; then For future, please take note of the error bash gives you which
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 http://stackoverflow.com/questions/3673594/how-to-escape-parenthesis-in-grep About Us Learn more about Stack Overflow the company Business Learn more about http://stackoverflow.com/questions/29473414/bash-syntax-error-on-if-statement-with-grep hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x 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 How to escape parenthesis syntax error in grep up vote 26 down vote favorite 1 I want to grep for a function call 'init()' in all JavaScript files in a directory. How do I do this using grep? Particularly, how do I escape parenthesis, ()? grep share|improve this question edited Dec 16 '15 at 21:33 Almo 9,11674372 asked Sep 9 '10 at 3:35 Megha Joshi - GoogleTV DevRel 6,37931717 add a grep syntax error comment| 4 Answers 4 active oldest votes up vote 26 down vote accepted It depends. If you use regular grep, you don't escape: echo '(foo)'|grep '(fo*)' You actually have to escape if you want to use the parentheses as grouping. If you use extended regular expressions, you do escape: echo '(foo)'|grep -E '\(fo*\)' share|improve this answer edited Dec 22 '11 at 8:21 Peter Mortensen 10.2k1369107 answered Sep 9 '10 at 3:37 Matthew Flaschen 175k28368450 1 When I use grep -rin init() * in the directory it complains syntax error near unexpected token (. How do I fix this. –Megha Joshi - GoogleTV DevRel Sep 9 '10 at 3:40 I am sorry, I do not understand. I have a few javascript files, with a init() function called in few places in them. I want to find out where all init() is called, using grep -rin init() * in the directory..It complains about invalid syntax near (. How do I escape ( . –Megha Joshi - GoogleTV DevRel Sep 9 '10 at 3:41 2 @Megha, just to clarify, that was a shell error because you didn't quote the regex. –Matthew Flasche
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 ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x 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 bash syntax error on if statement with grep up vote 0 down vote favorite I'm getting a syntax error on line 7 when i is equal to 0 (and if is supposed to fail). If I use it on files where grep succeeds and i is not 0 the program works. What could be the problem? #!/bin/bash function search_author_tests { while read line; do i=`grep -c "commit version: "$line"" "$1"/QA/*.tst` echo $i if (($i>0)); then echo -n $line grep "result" `grep -wl "commit version: "$line"" "$1"/QA/*.tst`|cut -d ":" -f2 fi done } grep -w "$2" "$1"/*.comp | cut -d" " -f2 | sort -n| search_author_tests $1 example error: line 7: ((: /u1/044/adaramir/mtm/ex1/q3/test3/cv/QA/1.tst:1 /u1/044/adaramir/mtm/ex1/q3/test3/cv/QA/2.tst:0>0: syntax error: operand expected (error token is "/u1/044/adaramir/mtm/ex1/q3/test3/cv/QA/1.tst:1 /u1/044/adaramir/mtm/ex1/q3/test3/cv/QA/2.tst:0>0") linux bash share|improve this question edited Apr 6 '15 at 14:47 asked Apr 6 '15 at 14:37 AtsrA 206 1 What syntax error are you getting? You should probably also run this code through shellcheck.net and correct the things it shows you. –Etan Reisner Apr 6 '15 at 14:42 1 This may not be the problem, but -- you need to change "commit version: "$line"" to just "commit version: $line". As it is, you're not actually protecting $line from filename-expansion and word-splitting. –ruakh Apr 6 '15 at 14:42 add a comment| 2 Answers 2 active oldest votes up vote 0 down vote You are grepping from several files, and you see the count per file. Change your assignment on line 7 into i=$(cat "$1"/QA/*.tst 2>/dev/null | grep -c "commit version: $line") share|improve t