开发者

Using regex with grep

开发者 https://www.devze.com 2023-01-19 09:26 出处:网络
One of my homework questions is to search using grep an lines that contain the word \'death\' or \'breath\'.

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

开发者_C百科

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


(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.


egrep '(d|br)eath' test_file


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

grep 'breath|death' test_file

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


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


egrep is FYI, deprecated. Use grep -E

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

精彩评论

暂无评论...
验证码 换一张
取 消