Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

syntax error near unexpected token `)'

by vit (Pilgrim)
on May 20, 2012 at 16:55 UTC ( #971498=perlquestion: print w/replies, xml ) Need Help??
vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I want to match with optional "'" and optional subsequent word
cat file.txt | perl -ne '{chomp; ($a, $b) = split/\s+/; $s = "$a $b"; + next if($s !~ /^\w+\'?\w+?\s+\w+\'?\w+?$/); print "$_\n"}' | wc -l
I am getting
bash: syntax error near unexpected token `)'
Is it something related to sygwin or activesatate Perl I use? Or I am missing something?

Replies are listed 'Best First'.
Re: syntax error near unexpected token `)'
by zwon (Abbot) on May 20, 2012 at 17:03 UTC

    It is something related to bash in fact. See QUOTUNG section in the manual:

    Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
      Then how to handle a single quote match?
        This sounds more like a bash question to me, but you can replace single quotes with \N{APOSTROPHE} or \x27. In bash you can do it like this:
        $ echo 'aa'"'"'bb'; aa'bb
Re: syntax error near unexpected token `)'
by tobyink (Abbot) on May 20, 2012 at 17:05 UTC
    Seems like a shell quoting issue rather than a Perl one. Try putting your script into a file rather than using the "-e" argument.
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      But the expression is correct and meets my purposes, right?

        You didn't tell us your purpose, so it's hard to say. But the error boils down to this: You can use either single or double quotes around an argument in the shell (bash, in this case), and either type can quote the other, but just as in Perl, there is no interpolation or escaping within single quotes. So:

        echo "'$SHELL'" -> outputs 'bash' echo '"$SHELL"' -> outputs "$SHELL"

        So if you need to include both kinds of quotes in a command line's arguments, you have a couple of choices: One is to use double quotes around the arguments and escape any double quotes within them. The problem with that is that you'll also need to escape other shell meta characters like ! and (), and that can lead to a complicated mess of backslashes. The other option is to switch back and forth between quote types as needed. You can do this in shell:

        echo "a ' quote"' and a " quote' outputs: a ' quote and a " quote

        So you can do your pipeline something like this, using single quotes around most of it but switching to double quotes around your literal single quotes:

        cat file.txt | perl -ne '{chomp; ($a, $b) = split/\s+/; $s = "$a $b"; + next if($s !~ /^\w+\'"'"'?\w+?\s+\w+\'"'"'?\w+?$/); print "$_\n"}' | + wc -l

        Aaron B.
        Available for small or large Perl jobs; see my home node.

        I would write it as:

        perl -lne '$s += /^\w+\047?\w+\s+\w+\047?\w+\s/ }{ print $s' file.txt

        Update: After re-reading the original post again that should be:

        perl -lne '$s += /^\w+(?:\047\w+)?\s+\w+(?:\047\w+)?/\s/ }{ print $s' +file.txt
Re: syntax error near unexpected token `)'
by Anonymous Monk on May 20, 2012 at 19:23 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://971498]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-12-17 01:31 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (459 votes). Check out past polls.