3

To retrieve strings of the following pattern from a text file populated with multiple entries:

entry=G_1234
entry=G_5678
entry=G_2345

The following grep statement doesn't provide any result

grep -ioe "entry=G_{1234,2345}" filename

Could you kindly guide as to where is the mistake or if parameter expansion is not possible in grep?

  • 1
    The parameter expansion will not take place between quotes. And if you manage to get it work how would grep use that expansion? Why not using regular expressions? – Nicola Sarobba Dec 21 '15 at 9:29
3

Either:

grep -iEo 'entry=G_(1234|2345)'

That is use the alternation operator of extended (E) regular expressions.

Or with shells that support brace expansion, use:

grep -io '-eentry=G_'{1234,2345}

The shell would then expand it to:

grep -io -eentry=G_1234 -eentry=G_2345

Causing grep to look for either of those 2 expressions.

| improve this answer | |
1

Grep uses regular expressions, not shell patterns. Regular expressions have different syntax:

grep -ioe 'entry=G_\(1234\|2345\)' filename
| improve this answer | |
  • Note that \| is a GNU extension as standard BRE don't have an alternation operator. (-o is a GNU extension as well anyway, so it's probably OK for the OP). – Stéphane Chazelas Dec 21 '15 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.