user2810895
user2810895

Reputation: 1364

regex for one character or another?

I'm searching for a regex code which lists all the lines that contain an a OR an i. I tryed this:

grep -E '[(a|i)]{1}' testFile.txt

but this gives me the words containing a or i and words that contain a en i. What's wrong?

Upvotes: 14

Views: 38594

Answers (2)

Alma Do
Alma Do

Reputation: 37365

You can achieve that with:

grep -E "^[^ai]*(a|i){1}[^ai]*$" testFile.txt

Upvotes: 9

halfbit
halfbit

Reputation: 3464

If you want an exclusive OR then try this: grep -E '^[^i]*a[^i]*$|^[^a]*i[^a]*$'

Upvotes: 0

Related Questions