So I understand that if you want to match beginning of a word with a specific letter(s), you would use \<, ex, \<Aa. Vice versa for end of a word, \>. How would you combine these two commands together?

for example, if you do \<Aag\> it will just match the word. Is there a way to separate these two commands?

  • From the questions, it is unclear what are you trying to accomplish: combine or separate? By the way, \< and \> are not part of POSIX standard for grep (I'd say they are GNU extensions), so the question should be tagged appropriately. Oct 19, 2023 at 15:18
  • I'm trying to find words that begin with ”qu” and end with ”y” But if I do \<quy\> that would just find the word 'quy' Oct 19, 2023 at 15:19

1 Answer 1


\< and \> operators are some non-standard regex operators found as extensions in some grep implementations with or without -E.

In most implementations, they match at the boundary between a non-word character (or the start of the subject) and a word character, and between a word character and a non-word character (or the end of the subject) respectively, word characters being alphanumeric characters or underscores.

Some grep/regexp implementations have [[:<:]] and [[:>:]] for that instead and some have \b (from perl) which matches either type of boundary.

Those that do support any of these generally also support the -w option which matches on words.

\<Aag\> matches inside foo-Aag as the \< matches inbetween - and A, and \> matches between g and the end of the subject (subject being lines for grep).

Some grep implementations also support \w to match any single word character. So with those, to find any word that starts with Aa and ends in g, you'd use \<Aa\w*g\>. The standard equivalent of \w would be [[:alnum:]_], so \<Aa[[:alnum:]_]*g\> would be slightly more portable. With some implementations, you may need \bAa[[:alnum:]_]*g\b or the equivalent with [[:<:]] and [[:>:]].

In standard grep, you could use:

grep -E '(^|[^[:alnum:]_)Aa[[:alnum:]_]*g([^[:alnum:]_]|$)'


grep -x '\(.*[^[:alnum:]_]\)\{0,1\}Aa[[:alnum:]_]*g\([^[:alnum:]_].*\)\{0,1\}'

with default BREs.

If your grep supports -P for perl regexps, then

grep -P '\bAa\w*g\b'


grep -Pw 'Aa\w*g'

Would work. Or you could use the real thing:

perl -ne 'print if /\bAa\w*g\b/'

(though beware that by default \w matches only on ASCII letters).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .