user1161080
user1161080

Reputation: 175

Why doesnt this grep command work

I'm trying to write a grep command that will match a python function declaration, in the form

def hello(hello)

so def, one space, a word that starts with either a letter, number or an underscore, open parenthesis, another word, closed parenthesis. I'm using the command

grep ^"def "[/-a-zA-z0-9][a-zA-z0-0]*[/(][a-zA-z0-9]*[/)]$ 

but I get a syntax error saying unrelated token near ( . I cant figure out what i'm doing wrong. any ideas?

Upvotes: 0

Views: 121

Answers (2)

JScoobyCed
JScoobyCed

Reputation: 10423

I think there are a couple of mistakes. Here is what should work:

grep '^def [_a-zA-Z0-9][a-zA-Z0-9 ]*[\(][a-zA-Z0-9 ][a-zA-Z0-9 ]*[\)]$'
  • I replaced your /- by underscore at the beginning of the regular expression
  • I adjusted the backslash parenthesis
  • I added some spaces in the classes in case it is needed
  • I added a '[a-zA-Z0-9]' after the opening parenthesis to guaranty at least one letter between parenthesis. You can remove it if you want to allow empty text in parenthesis

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272802

I think you want backslashes instead of forward slashes.

Upvotes: 1

Related Questions