0

One of my homework questions is to search using grep an lines that contain the word 'death' or 'breath'.

I know the [] operator acts as an or, but how do I use it to specify 'd' or 'br' ?

This won't work:

egrep [dbr]eath test_file

5 Answers 5

2

(d|br) is for either d or br.

The square brackets are for matching a single character from square brackets.

For example: [asdf]ello would match aello, sello, dello, or fello.

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

Comments

1

egrep '(d|br)eath' test_file

Comments

1

The "|" operator (vertical bar) acts as an "or". However:

grep 'breath|death' test_file

What you wrote would find "death", "beath", and "reath".

Comments

0

Study up on "alternation" and you'll figure it out.

Comments

0

egrep is FYI, deprecated. Use grep -E

grep -E '(d|br)eath' file

Comments

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.