1

I need to use a regular expression in grep inside a bash script where I am trying to find the following occurrences inside a file:

foo, foo , foo) foo )

In the file, there are multiple occurrences of this word in other patterns that I want to ignore, like:

foo" foo. etc

Right now I am doing individual grep for each, but I want to use a single expression for all of these. I tried 'foo.,' and 'foo.)', but I still need 2 expressions and it (obviously) gives me matches with any character at the place of . which I don't want. What can I use?

2 Answers 2

2
grep -e 'foo \?[,)]' file_to_check

You probably want the word boundary answer though.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to try & match comma, space, (, ) etc.

Just use foo with word boundaries:

grep '\<foo\>' file

To specifically match those 4 strings:

egrep '\<foo ?[,)]' file

7 Comments

Sorry, I think I phrased my question wrong. In a file, I want to find the four cases that I mentioned, and not just the word in the four cases !
@rgamber Please clarify: are there other instances of foo (not followed by ,, space, ( or )) that you would like to avoid matching?
Yes, there are. I'll update the question to say that.
It works fine when I run it directly in the terminal. When I use it in the bash script, that is when I get the error.
Can you post your BASH script so that I can investigate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.